#include #include using namespace std; int main() { string word; cout << "Enter a word followed by two ints: "; cin >> word; int x, y; cin >> x >> y; // string entire_line; // cin.getline( entire_line, 1000 ); // getline() reads a line of input, including whitespace // characters -- i.e. until user hits ENTER or 1000 characters // are entered.... // cout << "Here's what you typed in:\n"; // cout << entire_line << endl; if ( word == "favorite" ) { cout << "That's my favorite, too!\n"; } // word: // [0] [1] [2] [3] [4] [5] [6] [7] // ------------------------------------------------- // | f | a | v | o | r | i | t | e | // ------------------------------------------------- cout << "Length: " << word.length() << endl; // use subscript (or []) notation to access individual char cout << "The first character is: " << word[0] << endl; cout << "The third character is: " << word[2] << endl; // the last character is indexed by length minus one cout << "The last character is: " << word[ word.length() - 1 ] << endl; int len = (int)word.length(); cout << "Again, the last char is: " << word[ len-1 ] << endl; // LOOPS // (1) initialization int index = 0; // (2) condition while ( index < word.length() ) { // loop body cout << word[ index ] << endl; // (3) update index++; // add 1 to index } // word: // [0] [1] [2] [3] [4] [5] [6] [7] // ------------------------------------------------- // | f | a | v | o | r | i | t | e | // ------------------------------------------------- // for loop combines the three required parts into one line for ( int index = 0 ; index < word.length() ; index++ ) { // loop body: cout << "==> " << word[ index ] << endl; } // count from 1 to 100: int i = 1; while ( i <= 100 ) { cout << i << ' '; i++; } for ( int i = 1 ; i <= 100 ; i++ ) { cout << i << ' '; } // sum the numbers 1 to 100: int sum = 0; for ( int i = 1 ; i <= 100 ; i++ ) { sum += i; // sum = sum + i; } cout << "The sum is: " << sum << endl; // conditions of: if while for // == != < <= > >= return 0; }