/* Copyright: 2000, All rights reserved. Program: Strings Switch Version: 1.1 Created/Revised: File: stringswitch.cpp Programmer: C. S. Tritt, Ph.D. Course: CS-150 Assignment: STL Strings and Switch Construct Example Compiler: Microsoft Visual C++ 6.0 Target: Win32 Description: This program demonstrates the use of C++ STL strings and the switch construct. The problem solved involves asking the user for a size code (L+, L, M, S and S-) and displying the corresponding text (Extra large, Large, Medium, Small and Extra small, respectively. This program uses a somewhat odd approach for demonstration purposes. It does not do complete error checking. Revisions: There have been no major revisions of this program. Constants and Variables: See code for constant and variable descriptions. */ // Included files. #include #include // Macros for ANSI C++ logical operations in MSVC++ 6.0. #define and && #define or || #define not ! using namespace std; int main() { // Define constants. const char LARGE = 'L'; const char MEDIUM = 'M'; const char SMALL = 'S'; // Get input. cout << "Please enter a size code: "; string scode; cin >> scode; // Process input. cout << "Size: "; if (scode.length() > 2) { cout << "Invalid code: too many letters\n"; return 1; } else { switch (scode[0]) // The string [] operator returns a char for use in the switch. { case LARGE: if (scode.length() == 1) cout << "Large"; else if (scode.length() == 2) if (scode[1] == '+') cout << "Extra large"; else { cout << "Invalid code: expecting \"+\"\n"; return 2; } break; case MEDIUM: cout << "Medium"; break; case SMALL: if (scode.length() == 1) cout << "Small"; else if (scode.length() == 2) if (scode[1] == '-') cout << "Extra small"; else { cout << "Invalid code: expecting \"-\"\n"; return 3; } break; default: cout << "Invalid code.\n"; return 4; } } cout << '\n'; // Everything worked. return 0; }