home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter05 / mad-lib.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  1.8 KB  |  72 lines

  1. // Mad-Lib
  2. // Creates a story based on user input
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. string askText(string prompt);
  10. int askNumber(string prompt);
  11. void tellStory(string name, string noun, int number, string bodyPart, string verb);
  12.  
  13. int main()
  14. {
  15.     cout << "Welcome to Mad Lib.\n\n";
  16.     cout << "Answer the following questions to help create a new story.\n";
  17.     
  18.     string name = askText("Please enter a name: ");
  19.     string noun = askText("Please enter a plural noun: ");
  20.     int number = askNumber("Please enter a number: ");
  21.     string bodyPart = askText("Please enter a body part: ");
  22.     string verb = askText("Please enter a verb: ");
  23.     
  24.     tellStory(name, noun, number, bodyPart, verb);
  25.  
  26.     return 0;
  27. }
  28.  
  29. string askText(string prompt)
  30. {
  31.     string text;
  32.     cout << prompt;
  33.     cin >> text; 
  34.     return text;
  35. }
  36.  
  37. int askNumber(string prompt)
  38. {
  39.     int num;
  40.     cout << prompt;
  41.     cin >> num;
  42.     return num;
  43. }
  44.  
  45. void tellStory(string name, string noun, int number, string bodyPart, string verb)
  46. {
  47.     cout << "\nHere's your story:\n";
  48.     cout << "The famous explorer ";
  49.     cout << name;
  50.     cout << " had nearly given up a life-long quest to find\n";
  51.     cout << "The Lost City of ";
  52.     cout << noun;
  53.     cout << " when one day, the ";
  54.     cout << noun;
  55.     cout << " found the explorer.\n";
  56.     cout << "Surrounded by ";
  57.     cout << number;
  58.     cout << " " << noun;
  59.     cout << ", a tear came to ";
  60.     cout << name << "'s ";
  61.     cout << bodyPart << ".\n";
  62.     cout << "After all this time, the quest was finally over. ";
  63.     cout << "And then, the ";
  64.     cout << noun << "\n";
  65.     cout << "promptly devoured ";
  66.     cout << name << ". ";
  67.     cout << "The moral of the story? Be careful what you ";
  68.     cout << verb;
  69.     cout << " for.";
  70. }
  71.  
  72.