Now, I'm working on the next part of the project, and for some reason, my playAgain function and the while loop in main are not working. This part is supposed to actually display the story and ask if the user wants to play again. Can you take a look?
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/***********************************************************************
* Program:
* Project 10, Mad Lib Program
* Sister Unsicker, CS124
* Author:
* Lanie Molinar
* Summary:
* This program is the final 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. It then displays the
* completed story.
*
* Estimated: 10.0 hrs
* Actual: 20.0 hrs
* I could never get it to work, even though I followed the pseudocode and
* got tutoring twice.
************************************************************************/
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;
/***********************************************************************
* This function gets the file name from the user and passes it to the other
* functions.
***********************************************************************/
void getFilename(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> fileName;
cin.ignore();
}
/***********************************************************************
*This function turns the prompts in the file into questions and asks the user
* for the information.
***********************************************************************/
void askQuestions(char story[])
{
if (story[0] != '<' || !isalpha(story[1]))
return;
else
{
story[1] = toupper(story[1]);
cout << "\t" << story[1];
for (int iStory = 2; story[iStory] != '>'; iStory++)
{
if (story[iStory] == '_')
cout << " ";
else
{
story[iStory] = tolower(story[iStory]);
cout << story[iStory];
}
}
cout << ": ";
cin.getline(story, 256);
}
}
/***********************************************************************
* This function reads the Mad Lib file and passes information to the
* askQuestions function.
***********************************************************************/
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;
}
void displayStory(int wordCount, const char STORY[][33])
{
for (int numWords = 0; numWords < wordCount; numWords++)
{
for (int iStory = 0; iStory < strlen(STORY[numWords]); iStory++)
{
char character = STORY[numWords][iStory];
if (character != '<' && character != '>' && !isalpha(character))
{
if (ispunct(character))
cout << character;
switch (character)
{
case '#':
cout << endl;
break;
case '{':
cout << " \"";
break;
case '}':
cout << "\"";
break;
case '[':
cout << " '";
break;
case ']':
cout << "'";
}
}
else if (isalpha(character))
{
for (int numWords = 0; numWords = wordCount; numWords++)
cout << " " << STORY[numWords];
}
}
}
}
char playAgain()
{
char continue;
do
{
cout << "Do you want to play again (y/n)? ";"
cin >> continue;
}
while (continue != 'y' && continue != 'Y' && continue != 'n' &&
continue != 'N')
return continue;
}
/**********************************************************************
* The main function calls the getFilename and readFile functions.
***********************************************************************/
int main()
{
char story[256][33];
char fileName[256];
char continue = playAgain();
while (continue != 'n' && continue != 'N')
{
getFilename(fileName);
int wordCount = readFile(story, fileName);
displayStory(wordCount, STORY);
}
cout << "\nThank you for playing.\n";
return 0;
}
|
I'm getting these errors:
p10.cpp:129:21: error: expected , or ... before continue
char playAgain(char continue)
^
p10.cpp: In function char playAgain(char):
p10.cpp:132:11: error: expected primary-expression before continue
cin >> continue;
^
p10.cpp:132:11: error: expected ; before continue
p10.cpp:133:11: error: expected primary-expression before continue
while (continue != 'y' || continue != 'n' || continue != 'Y' ||
^
p10.cpp:133:11: error: expected ) before continue
p10.cpp:133:20: error: expected ; before != token
while (continue != 'y' || continue != 'n' || continue != 'Y' ||
^
p10.cpp:133:20: error: expected primary-expression before != token
p10.cpp:133:30: error: expected primary-expression before continue
while (continue != 'y' || continue != 'n' || continue != 'Y' ||
^
p10.cpp:133:30: error: expected ; before continue
p10.cpp:140:11: error: expected primary-expression before continue
return continue;
^
p10.cpp:140:11: error: expected ; before continue
p10.cpp:140:11: error: continue statement not within a loop
p10.cpp: In function int main():
p10.cpp:150:9: error: expected unqualified-id before continue
char continue = playAgain(continue);
^
p10.cpp:151:11: error: expected primary-expression before continue
while (continue != 'n' && continue != 'N')
^
p10.cpp:151:11: error: expected ) before continue
p10.cpp:151:20: error: expected ; before != token
while (continue != 'n' && continue != 'N')
^
p10.cpp:151:20: error: expected primary-expression before != token
p10.cpp:151:30: error: expected primary-expression before continue
while (continue != 'n' && continue != 'N')
^
p10.cpp:151:30: error: expected ; before continue