fork download
  1. //Cameron Pham CS1A Chapter 3, P. 148, #22
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Create a Word Game
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program plays a word game with the user. The user is asked to enter
  9.  * their name, age, city, college, profession, a type of animal, and the pet's
  10.  * name. The program creates and displays a short story using these inputs.
  11.  * _____________________________________________________________________________
  12.  *
  13.  * INPUT
  14.  * name: User's name
  15.  * age: User's age
  16.  * city: Name of city
  17.  * college: Name of college
  18.  * profession: Name of profession
  19.  * animal: Type of animal
  20.  * petName: Name of pet
  21.  *
  22.  * OUTPUT
  23.  * A personalized story using the input information.
  24.  *
  25.  ******************************************************************************/
  26. #include <iostream>
  27. #include <string>
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. // Declare the variables used to store the user's answers.
  33. string name;
  34. int age;
  35. string city;
  36. string college;
  37. string profession;
  38. string animal;
  39. string petName;
  40.  
  41. // Ask the user for their name, age, city, college, profession, type of
  42. // animal, and pet's name.
  43. cout << "Enter your name: " << endl;
  44. getline(cin, name);
  45.  
  46. cout << "Enter your age: " << endl;
  47. cin >> age;
  48.  
  49. // Clear the input buffer before using getline() again.
  50. cin. ignore();
  51.  
  52. // Continue asking for user input.
  53. cout << "Enter your city: " << endl;
  54. getline(cin, city);
  55.  
  56. cout << "Enter your college: " << endl;
  57. getline(cin, college);
  58.  
  59. cout << "Enter your profession: " << endl;
  60. getline(cin, profession);
  61.  
  62. cout << "Enter a type of animal: " << endl;
  63. getline(cin, animal);
  64.  
  65. cout << "Enter the pet's name: " << endl;
  66. getline(cin, petName);
  67.  
  68. // Display the completed story.
  69. cout << endl;
  70. cout << "There once was a person named " << name << " who lived in "
  71. << city << ". At the age of " << age << ", " << name
  72. << " went to college at " << college << ". " << name
  73. << " graduated and went to work as a " << profession << ". Then, "
  74. << name << " adopted a " << animal << " named " << petName
  75. << ". They both lived happily ever after!" << endl;
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0s 5308KB
stdin
Cameron
18
Laguna Niguel
Saddleback College
Student
Dog
Shelly
stdout
Enter your name: 
Enter your age: 
Enter your city: 
Enter your college: 
Enter your profession: 
Enter a type of animal: 
Enter the pet's name: 

There once was a person named Cameron who lived in Laguna Niguel. At the age of 18, Cameron went to college at Saddleback College. Cameron graduated and went to work as a Student. Then, Cameron adopted a Dog named Shelly. They both lived happily ever after!