Programming Challenge Chapter 3 – Q #22 Word Game – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 3 – Q #22 Word Game – Tony Gaddis – Starting Out With C++


Problem: -

Write a program that plays a word game with the user. The program should ask the user to enter the following:
  • His or her name 
  • His or her age 
  • The name of a city
  • The name of a college
  • A profession 
  • A type of animal
  • A pet s name
After the user has entered these items, the program should display the following story, inserting the user s input into the appropriate locations:
There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted a(n) ANIMAL named PETNAME. They both lived happily ever after!

Solution: -

#include <iostream>

#include <string>

using namespace std;

int main()

{

      string name, city, college, profession, animal_type, petname;

      int age;

      cout << "Enter Your Name: ";

      getline(cin, name);

      cout << "Enter Your Age: ";

      cin >> age;

      cin.ignore();

      cout << "Enter Your City Name: ";

      getline(cin, city);

      cout << "Enter Your College Name ";

      getline(cin, college);

      cout << "Enter Your Profession Name: ";

      getline(cin, profession);

      cout << "Enter Your Animale Type: ";

      getline(cin, animal_type);

      cout << "Enter Your Pet Name: ";

      getline(cin, petname);

      cout << endl;

      cout << "There once was a person named " << name << " who lived in " << city << ".";

      cout << "At the age of\n" << age << ", " << name << " went to college at " << college << ". ";

      cout << name << " graduate and went back to work\nas a " << profession << ". " << "Then, " << name << " adobted a(n) ";

      cout << animal_type << " named " << petname << ". They\nboth lived happily ever after!";

      return 0;

}

This is the solution of this question




OUTPUT OF THIS QUESTION





Explanation of this Solution

  1. Add a header file for string.
  2. Declare 6 string variables for input.
  3. Declare a int variables for input
  4. Take input one by one
  5.  In last, display output on the screen.
  6. Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File


Post a Comment

0 Comments