Hi, everyone. I'm working on a program that allows the user to play a game called a Mad Lib. I'm doing it in a couple parts. Right now, my code is supposed to read the Mad Lib file and ask the user for information to fill in parts of the story. For some reason, my code isn't outputting the questions it's supposed to ask, and I'm not sure why. I worked with a tutor from my school earlier today, and we fixed some things, but she had another appointment, so she couldn't help me keep working on it. Here's a comparison of what I'm getting, and what I should be getting, when I run my code through the tests it needs to pass:
Starting Test 1
This first test is trivial, reading the following file:
My pet cat is very <{> <adjective> . <}> <#> So is my dog . <#>
There is one prompt: "adjective" which needs to turn to "\tAdjective: "
> Please enter the filename of the Mad Lib: /home/cs124/projects/madLibTrivial.txt
> \t:
Exp: \tAdjective:
happy
>
Exp: Thank you for playing.\n
Test 1 failed.
------------------------------------------------------------
------------------------------------------------------------
Starting Test 2
This second test has 13 prompts. The file is:
I feel like yelling <{> <web_site_name> <}> every time I <verb> the <#>
Internet . So many <plural_noun> to look for , so many <plural_noun>
to find . <#> <#> My mother asked me , <{> <proper_noun> , what is so
<adjective> about <#> the Web ? <}> <{> Well , <}> I said , <{> just
the other day I did a search for <noun> <#> by entering <[> <noun>
<boolean_operator> <noun> <]> in a search <#> engine . And it returned
<favorite_website> and <another_website> . It's <#> just so <adjective>
! <}> <#> <#> And then she knew what I meant . I felt so happy ! <#>
> Please enter the filename of the Mad Lib: /home/cs124/projects/madLibWeb.txt
> \t:
Exp: Web site name:
Automobile Magazine.com
> \t: \t:
Exp: Verb:
drive
> \t:
Exp: Plural noun:
cars
> \t:
Exp: Plural noun:
motorcycles
> \t:
Exp: Proper noun:
New York
> \t: \t:
Exp: Adjective:
red
> \t:
Exp: Noun:
Porsche
> \t:
Exp: Noun:
BMW
> \t:
Exp: Boolean operator:
xor
> \t:
Exp: Noun:
Ferrari
> \t:
Exp: Favorite website:
Car and Driver
>
Exp: Another website:
Ferrari.it
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/***********************************************************************
* Program:
* Project 09, Mad Lib Program
* Sister Unsicker, CS124
* Author:
* Lanie Molinar
* Summary:
* This program is the second part of the Mad Lib project. It reads the Mad
* Lib from a file, making sure it's the right size, and prompts the user
* for words or phrases to fill in the needed text.
*
* Estimated: 6.0 hrs
* Actual: 0.0 hrs
* Please describe briefly what was the most difficult part.
************************************************************************/
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;
void getFilename(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> fileName;
}
void askQuestions(char story[])
{
if (story[0] != '<' || !isalpha(story[1]))
return;
cout << "\t";
story[1] = toupper(story[1]);
for (int iStory = 2; story[iStory] == '>'; iStory++)
{
if (story[iStory] == '_')
cout << " ";
else
story[iStory] = tolower(story[iStory]);
}
cout << ": ";
cin >> story;
}
int readFile(char story[][33], char fileName[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error reading file " << fileName << ".\n";
return 0;
}
int numWords = 0;
while (fin >> story[numWords])
{
askQuestions(story[numWords]);
numWords++;
}
if (numWords > 256)
{
cout << "Error reading file " << fileName
<< ". It has more than 256 words.";
return 0;
}
fin.close();
return numWords;
}
/**********************************************************************
* The main function tells a program where to start.
***********************************************************************/
int main()
{
char story[256][33];
char fileName[256];
getFilename(fileName);
int wordCount = readFile(story, fileName);
return 0;
}
|