// File: story.cxx // A sample program to illustrate more use of arrays. // The program writes a story (really!) //-------------------------------------------------- #include #include #include #include using namespace std; //-------------------------------------------------- //-------------------------------------------------- const int MANY_NOUNS = 6; const int MANY_ADJECTIVES = 7; const int MANY_NUMBERS = 9; const int MANY_SENTENCES = 5; //-------------------------------------------------- //-------------------------------------------------- // Function prototypes: void collect(string word[], int many, string what); //-------------------------------------------------- //-------------------------------------------------- int main( ) { string noun[MANY_NOUNS]; string adjective[MANY_ADJECTIVES]; string number[MANY_NUMBERS]; int i; collect(noun, MANY_NOUNS, "nouns"); collect(adjective, MANY_ADJECTIVES, "adjectives"); collect(number, MANY_NUMBERS, "numbers"); srand(time(0)); cout << "A Magic Story" << endl; cout << "by A. Computer" << endl; for (i = 0; i < MANY_SENTENCES; i++) { cout << "The " << noun[rand( ) % MANY_NOUNS] << " was very " << adjective[rand() % MANY_ADJECTIVES] << " for " << number[rand( ) % MANY_NUMBERS] << " years." << endl; } cout << "The (" << adjective[rand( ) % MANY_ADJECTIVES] << ") end." << endl; return 0; } //-------------------------------------------------- //-------------------------------------------------- void collect(string word[], int many, string what) { int i; // Loop control variable cout << "Please type " << many << " " << what << endl; for (i = 0; i < many; i++) { cin >> word[i]; } } //--------------------------------------------------