Mad libs program

I'm writing a program that reads txt file that has a mad libs story in it like this

Zoos are places where wild <plural_noun> are kept in pens or cages <#> so
that <plural_noun> can come and look at them. There is a zoo <#> in the park
beside the <type_of_liquid> fountain . When it is feeding time , <#> all
the animals make <adjective> noises . The elephant goes <{> <funny_noise>
<}> <#> and the turtledoves go <{> <another_funny_noise> . <}> My favorite
animal is the <#> <adjective> <animal> , so fast it can outrun a/an
<another_animal> . <#> You never know what you will find at the zoo. <#>

Where

# Newline character means No space before or after.
{ Open double quotes means Space before but not after.
} Close double quotes means Space after but not before.
[ Open single quote means Space before but not after.
] Close single quote means Space after but not before.
anything else A prompt

this is what the program is suppose to look like.

Please enter the filename of the Mad Lib: madlibZoo.txt
Plural noun: boys
Plural noun: girls
Type of liquid: lemonade
Adjective: fuzzy
Funny noise: squeak
Another funny noise: snort
Adjective: hungry
Animal: mouse
Another animal: blue-fin tuna
Zoos are places where wild boys are kept in pens or cages
so that girls can come and look at them. There is a zoo
in the park beside the lemonade fountain. When it is feeding time,
all the animals make fuzzy noises. The elephant goes "squeak"
and the turtledoves go "snort." My favorite animal is the
hungry mouse, so fast it can outrun a/an blue-fin tuna.
You never know what you will find at the zoo.


Then the program redisplays the mad libs story with all the corrections My problem is I don't know how to get it so that I let the user input the adjectives, nouns, and such. This is my program so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
using namespace std;

void readFile(ifstream& readFile, char nameofFile[], char emptyMadLibs[][])
{
  cout << "Please enter the filename of the Mad Lib: ";
  cin.getline(nameofFile, 256);
  readFile.open(nameofFile);
  for(int i = 0;! readFile.eof(); i++)
    {
      readFile >> emptyMadLibs[i][256];
      if(emptyMadLibs[i][256] == "<#>")
        emptyMadLibs[i][256] = " \n";
      else if(emptyMadLibs[i][256] == "<{>")
        emptyMadLibs[i][256] = " \"";
      else if(emptyMadLibs[i][256] == "<}>")
    emptyMadLibs[i][256] = "\" ";
      else if(emptyMadLibs[i][256] == "<[>")
        emptyMadLibs[i][256] = " \'";
      else if(emptyMadLibs[i][256] == "<]>")
        emptyMadLibs[i][256] = "\' ";
    }
}

int main()
{
   char nameofFile[256], emptyMadLibs[32][256];
   ifstream readFile;
   readFile(readFile, nameofFile, emptyMadLibs);
   return 0;
}
Last edited on
just ask the user for input and read it into variables exactly like you did with the name of the file. then insert the variables at the appropriate spot. eg:

1
2
3
4
5
6
cout << "Gimme a noun: "
cin >> noun;

//some code

cout << "blah blah " << noun << " blah blah\n";


you have lots of errors in that code, though
void readFile(ifstream& readFile, char nameofFile[], char emptyMadLibs[][])
second dimension of you 2-dimensional array needs to be explicitly stated

if(emptyMadLibs[i][256] == "<#>")
this is a c-string. == won't work. either use object strings or use the strcmp() function

edit:, actually, you're comparing an individual character in the array to a null terminated string. and the individual character isn't even within the bounds of the array. remember array indices start at 0, so your second dimension goes from 0-255. also remember that the last element of the array will hold either garbage or '\0'. you're probably going to need to rethink this and start over. i hope this helps, though

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
http://www.cplusplus.com/reference/clibrary/cstring/strncmp/
Last edited on
I would love to have it the way that you put it but my teacher wants us to read from the file and based off what the txt file says have them put the adjective and other things. I would know really where to start. my professor makes us write really redundant programs and I don't know how to figure it out.
what dose that program do ElectricClam148 ?
It reads reads a file that has a mad Libs story and then based on what nouns and adjectives are in the story we are suppose to ask them for the nouns adjectives and any other thing is ask for. Then it displays the mad libs with all of the adjectives the user inputed.
Topic archived. No new replies allowed.