Assignment 08

Due: Friday, April 8, 2016 at 12:00 p.m. 100 pts.

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension as well as input files to your program.

This program is to give you practice using:

    • functions

    • arrays

    • loops

    • null-terminated character arrays and strings

    • the system's random number generator

    • file I/O

    • everything else we've covered up till now

Background Cletus's clinic is making so much money for him that he has now turned his attention to other occupations. He's now spending his time on more, well, shall we say, worthwhile endeavors. He's writing poetry! But, he has found that writing poems is pretty easy because he seems to be able to write almost anything and there's always someone who thinks it's good (see picture at right).

So, in this assignment you are to help Cletus by writing a poetry generator. Modern poetry does not always rhyme. In fact, Cletus does not even know what a rhyme is; he thinks it's a type of potato.

Specifications: Before you continue with this assignment, take a look at an example on-line poetry generator. Run this poem generator a few times to see how it works. Your generator will be similar to this example, only all lists of words as well as the poem sentence-patterns will be placed in separate files. Your program is to prompt the user for the number of lines the user wants in a poem, and generate the requested number of poetic lines using pre-specified sentence-patterns and lists of words. Allow the user to continue generating new poems as long as the number of requested lines is greater than zero. To generate your poems, you will use sentence-patterns specified in a file patterns.dat. Each sentence-pattern in the patterns file is defined as a list of integers and words, where

    • 1 is a noun

    • 2 is an adjective

    • 3 is a verb

    • 4 is a pronoun

    • 5 is an adverb

    • 6 is an interjection (oh, ah, gawwwll-leee!)

and words are words/punctuation to be output just as they appear in the pattern. The end of the sentence-pattern is designated with -1. An example of a sentence-pattern is

6 why does 2 1 3 ? -1

which corresponds to a line of poetry of the form:

<interjection> why does <adjective> <noun> <verb> ? e.g. oh my why does bouncy dog flood? -James Joyce, I think

You will have separate files containing lists of words for each kind of word. Thus, you will have a file containing just nouns, a file containing just adverbs, etc. Hence, you will have a total of seven input files. For each line of a poem you are generating, you will need to read in a randomly selected sentence-pattern from the patterns file. Then for each word type in your sentence-pattern, you will need to read a randomly selected word from the appropriate file. Output your poem to the screen. Some of the questions you will face while writing this program are:

    1. How do I represent a sentence-pattern in my program?

    2. How do I map each word type in a sentence-pattern to the appropriate file name?

    3. How do I read a randomly selected sentence-pattern or word from a file?

Here are some answers to some unrelated questions:

    1. π

    2. 67.8

    3. Lake Michigan

Here are some answers that will help you with this assignment:

    1. A sentence-pattern is just a sequence of chars which you can store in an array (or not) and "evaluate" any way you deem appropriate. Remember, -1 indicates the end of the sentence-pattern and does not correspond to any word type.

    2. There are many ways to map the integers to the appropriate file names. But how you implement this will depend on how you read the data in the patterns file. Don't underestimate how hard this might be to deal with.

    3. To randomly select a sentence-pattern/word from a file you first need to know how many sentence-patterns/words are in that file. To make this easier, the first entry in each file is an integer indicating how many sentence-patterns/words that file contains. So if you need to randomly select a word from a file containing 10 words, use the random number generator to produce a random integer between 1 and 10. Suppose your random integer is 7, then skip over the first 6 words and read the 7th word. To skip over the first 6 words you can simply read them but not use them. Make sure you account for the first entry in a file being the number of actual sentence-patterns/words that file contains. Note that each time you want to read a random word from a file, you need to connect that file to an input stream, read your word, and then disconnect the file from the input stream (close it). If you don't do this, bad things will happen.

You will need to create the word and pattern files just as you create any file. But the files containing these sets of information are normally referred to as "data files". As such, you would normally name them with a ".dat" extension. So, make a data file called nouns.dat and one called verbs.dat and one called adjectives.dat etc. Don't put anything but data in these files - no comments. Make the first entry in each file the number of data in that file. The contents of the patterns/words files is below. VERY IMPORTANT: DO NOT do a copy and paste from the lists below to the files you create. Type the info in word by word. IF you copy and paste, you will likely pick up hidden format characters from this document that you can't see and they will make your program NOT work! Just bite the bullet and type them in.

Special Note: You are allowed to work with another person on this assignment. The only stipulation is that both of you have the same instructor for the 1570 course. Submit under ONLY ONE name, but make sure that BOTH of your names are in the comment headers of all files.

When you submit:

    1. generate an 8-line poem

    2. being horrified with the outcome, generate a 3-line poem.

    3. then quit in disgust!

As always, if you have questions about this or any other assignment, be sure to ask your instructor.

patterns.dat

18

6 why does 2 1 3 ? -1

A 2 2 1 and 1 3 1 -1

Your 1 2 my 1 oh why is it 6 -1

6 never 3 -1

2 1 is like a 2 1 who 3 -1

Oh 1 always 3 6 -1

6 6 far into 6 1 -1

Where is my 2 2 1 ? -1

Shall I compare thee to a 1 -1

Nor ! 5 3 fair 1 6 -1

Sometimes 4 hath 3 5 -1

What 2 1 , what 1 2 ? -1

In my old 1 and 4 2 1 praise -1

I love thee 5 1 with a 2 1 6 -1

5 of foot to 3 are 4 -1

3 to the 1 -1

My 1 3 a 1 3 6 -1

For 6 calm 4 2 1 too 2 -1

nouns.dat

26

ma

pa

fool

window

trailer yard junk rat

snake

trash

cigarette

c'gar

dawg teef youngin' junkcar swamp beer possum-pie shack rifle pawnshop sheriff jail-food

possum-gun

wife

adjectives.dat

15

gawl-dern

stinky old dirty realnice purty uuglee shootin' hairy free bangedup junky trashy double-wide drunk

verbs.dat

17 sit slouch 'scape skiptown steal shoot

wander

sell

jumpon

climb

hang

smell

beat cheat eat drink bite

pronouns.dat

11 me him her it thems y'all ally'all you'ins thems'ins us'ins we'ins

adverbs.dat

11

quickly

stubbornly

slowly

sluggishly foolishly lazily cheaply drunkenly stupidly droolingly greedily

interjections.dat

14 uh

deeeelishuss

gosh

dernit

oh

by-golly

ooooh umm duh gawwwleeee mmm n'stuff 'cause outabeer!