#include #include #include using namespace std; int main() { // (1) Read an integer from the user int n; cout << "Enter n (must be odd and in range [3,39]): "; cin >> n; // (2) Check to see if it's not valid // (3) If not valid, read an integer from the user again // and loop back up to step (2) // while n is less than 3 OR n is greater than 39 // OR n is an even number while ( n < 3 || n > 39 || n % 2 == 0 ) { cout << "Nope. Invalid. Try again: "; cin >> n; } cout << "Hey, thanks, that's a valid input.\n"; // the above code is an example of // input validation char letter = 'P'; string word = "computer"; cout << letter << endl; cout << word << endl; cout << word[3] << endl; for ( unsigned int i = 0 ; i < word.length() ; i++ ) { cout << "Character at index " << i << " is " << word[i] << endl; } cout << "Enter a single word: "; cin >> word; // cin will read characters until it // encounters a space (' ' or '\t') cout << "You entered " << word << '.' << endl; cin >> word; cout << "Then you entered " << word << '!' << endl; // ignore the next set of characters // until we see a '\n' character // ignore a maximum of 1000 characters cin.ignore( 1000, '\n' ); cout << "Entire a sentence:" << endl; string line; getline( cin, line ); // getline() reads an entire line of input // (not including the '\n') and stores // the entire string in the line variable cout << "You typed in: " << line << endl; if ( isalpha( line[0] ) ) // this is from { cout << "The first char you typed is a letter."; cout << endl; } if ( isupper( line[0] ) ) { cout << "First char is UPPERCASE letter\n"; } // isalpha() isupper() islower() // isdigit() isspace() ispunct() // Write a for loop to print the line variable // in reverse for ( int i = (int)line.length() - 1 ; i >= 0 ; i-- ) { cout << line[i]; } cout << endl; // Write a for loop to print each lowercase letter // in the line variable (i.e. skip all others) for ( int i = 0 ; i < (int)line.length() ; i++ ) { if ( islower( line[i] ) ) { cout << line[i]; } } cout << endl; return 0; }