Assignment 08

Due: Wednesday, November 9, 2011, at noon, 100 points

For this assignment, you will submit multiple files containing a program written in C++. You know how to use the submit program by now; I needn't tell you.

The following is a list of topics we wish to address with this assignment:

    • null-terminated character arrays

    • array and character modification

    • array and character input/output

    • file I/O

    • funny randomization

Resources: www.cplusplus.com Check out the different libraries, especially the iostream library.

Background: Willie's mother used to play a game with him when he was young. It worked like this. She would write a short story (she was a journalist, so it was easy for her) in which she left blanks for various types of words: nouns, adjectives, verbs, etc. Then, she would ask him, in the order that they appeared in the story, to supply a noun, verb, or adjective. He'd pick some word at random and she'd write it in. So, she might ask for a noun and he'd respond, "elephant". Of course, when the story was finished, she would read it to him and they'd both laugh. The idea was that the words he supplied would sound ridiculous in the sentence. So, this is what you will program: this game. You see, Willie's mom has passed and he wants to relive those days....since he ___________________________________ (<-- just fill in some story line, I'm out of ideas here.)

Specifications: You are to write a program that emulates the above game. (I've been told that it is sold commercially now under the name "madlibs".) Your program is to read a story from one file (say, "story.dat"). It is to print it to the screen unchanged. After this, your program is to then replace the words which have been "marked" with the appropriate type of word (noun, verb, etc.). A short message announcing the completion of this task will then be followed by the changed story.

So, how is this to be accomplished? There will be five files containing lists of words (nouns, verbs, adjectives) from which you will randomly choose a word to replace the marker in the story. The markers will be one of the following: *nounp* or *nouns* or *verb* or *verbp* or *adj*. nounp is a plural noun, nouns is singular, verbp is present tense verb and verp is a past tense verb. adj is an adjective. When you "see" one of these, replace it (*'s included!) with the appropriate kind of word. You'll have to draw at random from the appropriate file. I'd suggest naming the files nouns.dat, nounp.dat, verb.dat, verbp.dat, and adjective.dat. Each of these files will begin with an integer equal to the number of words in the list contained therein. I will give you lists below to start with, but you can add to them if you like (make the words fit the type and make them clean! Also, don't make any list more than, say, 20 words.) Of course, after you've submitted this, you can change things and have fun with it.

As indicated, randomness is to be used in this assignment. You know how to use the random number generator supplied by the compiler. You will have to figure out just how you will use it in this program. There are some really tricky and cool ways to use C++ to do what you need to do here. I'm going to let you think about it and experiment. Ask the right questions and you can develop some fine code. Clearly, the leading number in the file will be key in how you use the random number generator. One key piece of information you need is this. Once you close a file, the "pointer to the data elements" will be set back to the first data element in the file. So when you open the file again, the "pointer" is pointing to the first data. There are tools that allow you to move the "pointer" to specific locations, but you don't need to know these. You have already the ability to do this with what you learned several weeks ago - you just have to be clever.

Since I want you to practice using NTCAs, I will demand that you use them and not Standard strings.

Submitting: When you submit, there is no user input, so just let 'er go.

Here are some lists and a story to work with. You can add to the lists, but don't change the story so that the grader has consistency to grade.

nouns nounp verb verbp adj 10 10 10 10 10 hat tables jumping jumped smart nail books sitting sat stupid dog bugs eating ate red trashcan cows biting bit jealous computer people kicking kicked frantic coat streets saying said crazy chair cables spitting spat trashy goat dogs driving cried goofy bag stars reading hit big hair telephones throwing tore smelly ...and the story: "Let's go *verb* in the *nouns*!" said Avery. The *nounp* *verbp* to the *nouns*. Mr. Zuckerman had the *adj* *nouns* in the country. It was a *adj* *adj* *nouns* tied to *nouns* over the north doorway. At the *adj* end of the *nouns* was a *adj* *nouns* to sit on. It was arranged so that you could swing without being *verbp*. You *verbp* a *nouns* to the *nouns*. Then, *verb* the *nouns*, you stood at the edge and *verbp* down, and were *adj* and *adj*. Then you *verbp* the knot, so that it acted as a *nouns*. Then you got up all your nerve, *verbp* a deep *nouns*, and *verbp*. For a second you seemed to be *verb* to the *nouns* far below, but then suddenly the *nouns* would begin to catch you, and you would be *verbp* through the *nouns* *verb* a mile a minute, with the *nouns* *verb* in your eyes and ears and *nouns*. Then you would be *verb* upward into the sky, and be *verb* up at the *nounp*, and the *nouns* would twist and you would be *verb* and *verb* with the *nouns*. Then you would be *verb* down, down, down out of the *nouns* and come *verb* into the *nouns* almost into the *nouns*, then sail out again (not quite so far this time), then in again (not quite so *adj* ), then out again, then in again, then out, then in; and then you'd be *verb* off and fall down and let *nouns* try it.

Now remember, there are many ways to work out the problems in programming this assignment. Think carefully about your options and don't be afraid to ask questions.

As always, if you have questions, don't hesitate to ask your instructor, or ask the lab help in the LEAD guys in the evenings.