fork download
  1. //Sam Partovi CS1A Chapter 3, P. 148, #22
  2. //
  3. /*******************************************************************************
  4. *
  5. * GENERATE PERSONALIZED STORY
  6. * ____________________________________________________________
  7. * This program generates a personalized story with given user information.
  8. * The given information is substited into a template story and outputs
  9. * the result.
  10. * ____________________________________________________________
  11. *INPUT
  12. * name : The user's name
  13. * cityName : The name of the user's city
  14. * age : The user's age
  15. * profession : The user's profession
  16. * animal : A type of animal chosen by the user
  17. * petName : The name to be assigned to the animal
  18. *
  19. ******************************************************************************/
  20.  
  21. #include <iostream>
  22. #include <string>
  23.  
  24. using namespace std;
  25.  
  26. int main() {
  27.  
  28. //Initialize program variables
  29. string name;
  30. string age;
  31. string cityName;
  32. string collegeName;
  33. string profession;
  34. string animal;
  35. string petName;
  36.  
  37. //Prompt user for inputs
  38. cout << "Enter your name: ";
  39. getline(cin, name);
  40.  
  41. cout << "Enter your age: ";
  42. cin >> age;
  43. cin.ignore();
  44.  
  45. cout << "Enter the name of a city: ";
  46. getline(cin, cityName);
  47.  
  48. cout << "Enter the name of a college: ";
  49. getline(cin, collegeName);
  50.  
  51. cout << "Enter a profession: ";
  52. getline(cin, profession);
  53.  
  54. cout << "Enter a type of animal: ";
  55. getline(cin, animal);
  56.  
  57. cout << "Enter a pet's name: ";
  58. getline(cin, petName);
  59.  
  60. //Generate the story
  61. cout << "\nThere once was a person named " << name
  62. << " who lived in " << cityName
  63. << ". At the age of " << age
  64. << ", " << name
  65. << " went to college at " << collegeName
  66. << ". " << name
  67. << " graduated and went to work as a " << profession
  68. << ". Then, " << name
  69. << " adopted a(n) " << animal
  70. << " named " << petName
  71. << ". They both lived happily ever after!" << endl;
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0.01s 5280KB
stdin
Sam
19
Mission Viejo
Saddleback
chemist
giraffe
Jason
stdout
Enter your name: Enter your age: Enter the name of a city: Enter the name of a college: Enter a profession: Enter a type of animal: Enter a pet's name: 
There once was a person named Sam who lived in Mission Viejo. At the age of 19, Sam went to college at Saddleback. Sam graduated and went to work as a chemist. Then, Sam adopted a(n) giraffe named Jason. They both lived happily ever after!