//----------------------------------------------------------------------------- // FILE: author.cxx // A demonstration program showing how the vector template class is used. // The program reads some words into vectors of strings. // Then a silly story is written using these words. #include // Provides assert #include // Provides cout and cin #include // Provides EXIT_SUCCESS and rand #include // Provides string class #include // Provides the vector template class using namespace std; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- const size_t ITEMS_PER_VECTOR = 4; // Number of items to put into each vector const size_t MANY_SENTENCES = 3; // Number of sentences in the silly story //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // PROTOTYPES // Prompt the user to read n strings that are described by description, // and put those strings into the vector. void get_items(vector& collection, size_t n, string description); // Precondition: v.size( ) > 0; // The return value is a random string selected from v string random(vector v); //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- int main( ) { vector adjectives; // Contains adjectives typed by user vector ages; // Contains ages in the teens typed by user vector names; // Contains names typed by user size_t line_number; // Number of the output line // Fill the three vectors with items typed by the program's user. cout << "Help me write a story.\n"; get_items(adjectives, ITEMS_PER_VECTOR, "adjectives that describe a mood"); get_items(ages, ITEMS_PER_VECTOR, "integers in the teens"); get_items(names, ITEMS_PER_VECTOR, "first names"); cout << "Thank you for your kind assistance.\n\n"; // Use the items to write a silly story. cout << "LIFE\n"; cout << "by A. Computer\n"; for (line_number = 0; line_number < MANY_SENTENCES; ++line_number) { cout << random(names) <<" was only " << random(ages) << " years old, but he/she was " << random(adjectives) << "." << endl;; } cout << "Life is " << random(adjectives) << "." << endl; cout << "The (" << random(adjectives) << ") end" << endl; return EXIT_SUCCESS; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- void get_items(vector& collection, size_t n, string description) { string user_input; // An item typed by the program's user cout << "Please type " << n << " " << description; cout << ", separated by spaces.\n"; cout << "Press the key after the final entry:\n"; do { cin >> user_input; collection.push_back(user_input); --n; } while (n > 0); cout << endl; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- string random(vector v) { assert(v.size( ) > 0); return v[rand() % v.size( )]; } //-----------------------------------------------------------------------------