COMP 142 Program 7: Madlibs
Assigned: Friday, April 10
Due: Sunday, April 19, 2015 by 11:55pm (on Moodle)
In this assignment, you will write a program to play the popular childhood game of Madlibs. MadLibs is a phrase/template word game where the user is prompted to substitute words in a story, before reading the actual story. Because the player is unaware of the story, the words entered often create a funny result. You can read about it in greater detail on Wikipedia: http://en.wikipedia.org/wiki/Mad_Libs. Here's a sample you can try out on your roommate: http://www.llemonade.com/DrFrankmadlib.pdf.
Your program will read the contents of a text file that contains the appropriate needed parts of speech as well as the story. You should prompt the user to enter in the name of a text file (and your program should continue to ask the person to enter a text file name until they enter a valid filename. Your MadLibs implementation will allow the user to play multiple times until the user indicates that they are ready to quit. You should display your instructions to the user at the *START* of the game.
The main driver of this program will define the needed variables and the functions should do most the work. I will be specifically looking at how well you break your code up into reasonable "chunks." Constant global variables are the only legal global variables. Main is a good place to delete any memory that has been dynamically allocated.
You are required to write the following three functions (you will have additional functions):
- A readGameFile function is to ask the player for the name of the file (including the extension) they would like to use. There are several **input files available in my public folder for you to use. Be sure to test that the file opened correctly. If it did not open, print an informative message to the screen and allow the user to enter another filename.
After successfully opening the input file, this function is to read all the information from the file and store it so it can be effectively used by other functions.
The input file consists of a number indicating how many inputs are required by the user for this Mad Lib followed by a list of what type of entries are needed, each on a separate line. You will read these entries into a dynamically allocated string array. Next, there is a number indicating how many lines of the story that there are. This is followed by the story, with each line of the story on a separate line. You will read in the story into a dynamically allocated string array.
An example of an input file: madlibs1.txt
You must use the following prototype for this function:
void readGameFile(string **entries, int *num_entries, string **story, int *num_lines);
- A getUserInput function used to get the appropriate input from the user. This data should be stored as a dynamically allocated array of strings.
- A createStory function that puts the user input into the story. You should look at this demo (stringfun.cpp) (also in my public folder) to help you with this portion.
- A print function is to print the story to the screen. The output is to be displayed nicely with a blank line between each line of the story.
For example, if the user entered nutty, Tina Turner, 37, and sardines, the printed output for the input file shown above would be:
The nutty computer science professor reminds me
of Tina Turner. I bet she has 37 dachshunds and eats
sardines every day.
Coding style
- Use good indentation. Just because C++ lets you get away with bad indentation doesn't mean you
should. Stick to the same indentation style and placement of curly braces throughout your code.
- All of the same coding style guidelines from the first project still apply, including
the rules on commenting your code.
- Don't forget to include your program header - it goes above your #include statements.
Testing your program
You should test your program thoroughly to make sure it works correctly.
What to turn in
Through Moodle, turn in your code as a file called madlibs_yourLastName_yourFirstName.cpp.
You do not need to turn in anything other than the .cpp file; I do not need all the other
files that Visual Studio creates.