Challenge 16

Description

The purpose of this challenge is to read and write files. This challenge creates a MADLIBS story from four input files.

Requirements

      1. Using vi, create four text files as below:
        • words.txt. When you type this out, make sure to place each word in a separate line to simplify your ability to read from it. You may use this story, or create your own as long as you have the same number of placeholders for each of adjectives, verbs and nouns. The words with the @ symbol are placeholders that will be replaced by words in the nouns, verbs and adjectives file.
        • I was at the store one particularly @adjective day. Inside, the cashier appeared to be @adjective yet @adjective. The @noun she wore wasn't exactly @adjective. I @verb her and she @verb me. I asked her where I could find a @noun. That upset her and she threw a @noun at me. I grabbed the nearest @noun and @verb it. I think I'll go somewhere else next time.
        • It’s easier to type out the file as above then press Enter at each space to create a new line. The words.txt file should look like this
        • I
          was
          at
          the
          store
          one
          particularly
          ....
        • nouns.txt (you can use these words or your own). This file must only contain 4 words
          carrot
          flag
          piano
          paintcan
        • verbs.txt. (you can use these words or your own). This file must contain only 3 words. All verbs must be past tense.
        • greeted
          threw
          spun
        • adjectives.txt. (you can use these words or your own). This file must contain only 4 words.
        • high
          minty
          wicked
          brown
      2. Create four string arrays nouns[4], adjectives[4], verbs[3], words[400]
      3. Create a function int load_file(char filename[], string items[]). Use this function call to load the nouns, adjectives, verbs and the MADLIB story from words.txt. This function returns how many line were loaded from the file. This return value is essentially the size of the items array each time it’s called. This function will call the getline() function to repeatedly read a line of text from the file whose filename is passed in the first parameter. Here’s how you might call the function in main:
      4. {
          int words_count, nouns_count, adj_count, verbs_count;
          
          nouns_count = load_file("nouns.txt", nouns);
          adj_count = load_file("adjectives.txt", adjectives);
          verbs_count = load_file("verbs.txt", verbs);
          words_count = load_file("words.txt", words);
        }
      5. Create the completed MADLIBS story by merging the nouns, adjectives, verbs into the story. See step 6 for details.
      6. Create a string called story. Use this variable to concatenate the words from the words array. Use a loop to iterate through the words array and match the appearance of the @noun placeholders with the next noun in sequence with the nouns array. Do the same for @verbs and @adjectives. (In the loop, if you encounter a @noun placeholder in the words array, grab the next noun from the nouns array. If you encounter a @verb placeholder, grab the next verb, etc)
      7. Write the completed MADLIBS story (the story string variable) into a file called madlibs.txt

Sample Interaction

There is no user interaction. After compiling and running your program, type:

$ cat madlibs.txt

This should show the contents of your completed story.

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0016

Print Requirements