Date: Mon, 11 Nov 1996 17:00:35 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Fri, 08 Nov 1996 21:04:59 GMT Content-length: 3563 Program 6

Program 6

For this assignment, you will write a program that can create "Mad Libs." A "Mad Lib" is a humorous story generated by taking otherwise normal paragraphs and substituting arbitrary words and phrases.

Mad Libs are probably best illustrated with an example. Here is a sample run of the Mad Libs program:

Enter a Mad Libs file: test.mad

Enter a number: 1127
Enter an adjective: slimy
Enter a person's name: Joe Blow
Enter an adjective: drunk
Enter a verb: incinerate
Enter an adjective: tranquil

Computer Science 1127 is a very slimy class.  The
reason is that our teacher, Joe Blow, is really
drunk.  One time, he even stopped right in the middle
of class to incinerate a student!  It wus truly a tranquil
experience.

The actual stories that will be processed by the Mad Libs program are stored in a separate text file. These Mad Libs files contain the prompts for each word or phrase to be substituted. For example, the Mad Libs file, test.mad, used in the above example looks like this:

Computer Science [a number] is a very [an adjective] class.  The
reason is that our teacher, [a person's name], is really
[an adjective].  One time, he even stopped right in the middle
of class to [a verb] a student!  It was truly a [an adjective]
experience!

Notice how the prompts are surrounded with square brackets. Your program should read the file (you can use the read_file function from Program 5), prompt the user for each of the words or phrases to substitute into the story, and then print out the story with the user's responses in place of the square-bracketed prompts.

Hints and Notes

Probably the simplest way to do this is to scan the Mad Libs file in two passes. The first time through, just extract each prompt, ask the user for their response, and store the response in an array. Then go through the text of the Mad Libs file a second time, this time printing out the story, substituting the user's responses for the square-bracketed prompts.

Recall that you can use the substring member function of the string class to extract portions of a string (e.g., to extract the prompts inside the square brackets.) For example,

   string x = "abcde";
   cout << x.substring (1,2); // prints "bc" - the string of 2 characters
                              // starting at position 1

Note: You do not have to worry about somehow "reformatting" the paragraph so all the lines have the same length. Just print the story as it is given in the file, substituting the user prompts with the user responses.

What to Hand In

As usual, you will hand in the source code, along with the output generated for some test cases. These test cases are now available. (Note: They are also available "directly" in the lab, as r:\public\mbirk\madlib1.txt and r:\public\mbirk\madlib2.txt.)

Feel free to hand in any of the creative madlibs that you came up with on your own! (But this is not required - only test cases 1 and 2 must be handed in.)


mbirk@cs.wisc.edu