Developing Game in C++: Mad Lib

20140709_125222Mad Libs are short stories written using words supplied by the person playing the game.  It is a phrasal template word game invented in the United States where one player prompts another for a list of words to substitute for blanks in a story, before reading the – often comical or nonsensical – story aloud. (Wikipedia)


Step 1: The Madlib program will prompt the user for the following information:

1. Name of person
2. Two different colors
3. Name of two body parts
4. A Noun
5. Name of an Animal
6. A Verb (ending with ‘ing’)
7. Two large numbers


Step 2: Once the user provides the information, the program will dynamically create the following story based on the response received from the user to the above-listed items:

My name is <Name> .  I am a(n) <Animal> that has <Color1> fur with <Color2> spots on its
<Body1>. I have a <Body2> shaped like a(n)  <Noun> which I use for <Verb> I weigh more than <Number1> pounds and I am <Number2> feet tall.


Step 3: Following is Madlib code in C++ language that will dynamically generate the story based on user’s input:

#include <iostream>
#include <string>
using namespace std;

int main() {

       // Declaration

string Name, Color1, Color2, Body1, Body2, Noun, Animal, Verb;
int Number1, Number2;
string Story;

 cout << “MADLIBS\n”;
cout << “Enter your name: “;
cin >> Name;
cout << “Enter your favorite color: “;
cin >> Color1;
cout << “Enter aother color: “;
cin >> Color2;
cout << “Name a body part: “;
cin >> Body1;
cout << “Name another body part: “;
cin >> Body2;
cout << “Enter a noun: “;
cin >> Noun;
cout << “Name an Animal: “;
cin >> Animal;
cout << “Enter a verb: “;
cin >> Verb;
cout << “Enter a large number: “;
cin >> Number1;
cout << “Enter another large number: “;
cin >> Number2;

// Building the story
cout << “=============================== My STORY =====================”;

Story = “My name is ” + Name + “. ” + ” I am an(n) ” + Animal + ” that has ” + Color1;
Storty +=
 ” fur with ” + Color2;
Story += ” spots on its ” + Body1 + “. I have a ” + Body2 +” shaped like ” + Noun;
Story += ” which I use for ” + Verb;
Story += “. I weigh more than ” + to_string (Number1);
Story += 
” pounds, and I am “ + to_string (Number2) + ” feet tall.”;

//Print the story
cout << Story;
}


Step 4: Saave the code in a c.pp file and compile the program. To compile the program, you will need a C++ compiler. If you do not have a C++ compiler then you can use C++ online compiler at www.cpp.sh. Just copy the code and past it in the C++ shell.

Step 5: Run the program. (If you are using online C++ compiler (www.cpp.sh) the click on Run button to run the program.

Step 6: The program will prompt the user for questions. Answer the question and see it build a Madlib story for you.

This is what the program will look when you run it:

MADLIBS

Enter your name: John

Enter your favorite color: red

Enter another color: blue

Name a body part: hand

Name another body part: leg

Enter a noun: house

Name an Animal: dog

Enter a verb: running

Enter a large number: 9000

Enter another large number: 99999

=============================== My STORY =====================

My name is John.  I am an(n) dog that has red fur with blue spots on its hand. I have a leg shaped like a House, which I use for running. I weigh more than 9000 pounds and I am 99999 feet tall. 


If you interested in learning a programming language (C, C++, Java, PHP and more), visit http://www.nrclc.com/computercamp/ or call us at (714) 505-3475