/////////////////////////////////////////////////////// // // decisions.cpp // // Started 1/6/97 by Jeff Ondich // Last modified: 1/6/97 // // 1. Read through this program and predict its behavior. // Compile it and run it several times with varying // input. Can you get all of the different output // messages? // // 2. If you comment out the "#include " line, // which symbols does the compiler fail to recognize? // // 3. isalpha() and isdigit() are two of several very // useful character-related functions available via // the ctype.h header file. Type "man ctype" at the // UNIX prompt to see the list of available functions. // // 4. Note the defined constant "kMaxStringLength". I will // normally name my defined constants in this way, // starting with a "k". This helps distinguish // constants from variables. When (if) I use global // variables, their names will start with "g". // These conventions are commonly used by professional // programmers, and some companies even require their // programmers to use conventions of this sort. // //////////////////////////////////////////////////////// #include #include const int kMaxStringLength = 20; int main( void ) { // // Part I. Playing with a string. // char aString[kMaxStringLength]; cout << "Type something: "; cin.getline( aString, kMaxStringLength ); cout << "Your string is " << strlen(aString) << " characters long." << endl; if( strlen(aString) == 0 ) { cout << "Yow! That's a short string." << endl; } else if( isupper(aString[0]) ) { cout << "Yipee! Your string's first character is a capital letter!\n"; } else if( isdigit(aString[0]) || islower(aString[0]) ) { cout << "Oooh! A digit or a small letter at the beginning!" << endl; } else { cout << "No letters, no digits. Woe is me." << endl; } // // Part II. Playing with a character. // char c; cout << endl << "One character, please: "; cin >> c; switch( c ) { case 'A': case 'a': cout << "That's a good beginning letter." << endl; break; case '!': cout << "UNIX programmers often say \"bang\" when they mean \"!\"." << endl; break; case '?': cout << "I don't know either." << endl; break; default: cout << "I have nothing to say about that character." << endl; break; } return( 0 ); }