I am having a problem here and I feel like that I am getting close.
here is the Error. (the lines with the Exp is what it should be)
> Please enter the filename of the Mad Lib: file.txt
> Adjective: happy > 16\n //why is there a number here?
Exp: \n > My pet cat is very " happy. " \n //why is there spacing?
Exp: My pet cat is very "happy."\n > So is my dog. \n //more spacing issues.
Exp: So is my dog.\n > Do you want to play again (y/n)?//not supposed to show in this case.
Exp: \n
Timed out!
#include <iostream>
#include <fstream> // used for getting and reading a file.
usingnamespace std;
void getFileName(char fileName[]);
int readFile(char madLibStory[][32]);
void askQuestions(char prompt[], int count);
void getPunctuation(char punc[]);
void display (char madLibStory[][32], int numWords);
/**********************************************************************
* Main() will contain the code that will call the other functions
* as well as the prompt for asking the user to play again or not.
***********************************************************************/
int main()
{
char madLibStory[256][32];
int numWords;
char yesOrNo;
bool playAgain = true;
while (playAgain)
{
numWords = readFile(madLibStory);
cout << numWords << endl;
display(madLibStory, numWords);
cout << "Do you want to play again (y/n)? ";
cin >> yesOrNo;
if (yesOrNo == 'n')
{
playAgain = false;
cout << "Thank you for playing." << endl;
}
elseif (yesOrNo == 'y')
playAgain = true;
elseif (yesOrNo != 'y' || yesOrNo != 'n')
{
cout << "Invalid entry. Do you want to play again (y/n)? ";
cin >> yesOrNo;
}
}
}
/**********************************************************************
* getFileName() will ask the user the filename
* of the text file.
***********************************************************************/
void getFileName(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> fileName;
}
/**********************************************************************
* readFile() this function is supposed to read the Mad Lib file and
* check for Error for incorrect reading and the loop for seeing the
* carrots.
***********************************************************************/
int readFile(char madLibStory[][32])
{
char fileName[256];
getFileName(fileName);
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error reading file: " << fileName << endl;
return -1;
}
int count = 0;
int numWords = 0;
while (numWords < 256 && fin >> madLibStory[numWords])
{
if (madLibStory[numWords][0] == '<' &&
isalpha(madLibStory[numWords][1]))
{
askQuestions(madLibStory[numWords], count);
count++;
}
elseif (madLibStory[numWords][0] == '<' &&
!isalpha(madLibStory[numWords][2]))
getPunctuation(madLibStory[numWords]);
numWords++;
}
fin.close();
return numWords;
}
/**********************************************************************
* askQuestions() will display the text from the file and ask the user
* the questions that are needed for the game.
***********************************************************************/
void askQuestions(char text[], int count)
{
cout << "\t" << (char)toupper(text[1]);
for (int i = 2; text[i] != '>'; i++)
{
if (text[i] == '_')
cout << " ";
else
{
cout << (char)tolower(text[i]);
}
}
cout << ": ";
if (count == 0)
{
cin.ignore();
cin.getline(text,256);
}
elseif(count > 0)
cin.getline(text,256);
return;
}
/**********************************************************************
* getPunctuation() will make sure that there are quotation marks
* inputted into their places correctly.
***********************************************************************/
void getPunctuation(char punc[])
{
{
switch (punc[1])
{
case'#':
punc[0] = '\n';
punc[1] = '\0';
break;
case'{':
punc[0] = ' ';
punc[1] = '\"';
punc[2] = '\0';
break;
case'}':
punc[0] = ' ';
punc[1] = '/"';
punc[2] = '\0';
break;
case'[':
punc[0] = ' ';
punc[1] = '\'';
punc[2] = '\0';
break;
case']':
punc[0] = '\'';
punc[1] = ' ';
punc[2] = '\0';
break;
}
}
return;
}
/**********************************************************************
* display will loop through the words in the function and insert the
* quotations for the words that the user inputed.
***********************************************************************/
void display (char madLibStory[][32], int numWords)
{
for (int i = 0; i < numWords; i++)
{
if (i == 0)
cout << madLibStory[i];
elseif (madLibStory[i][0] == '.' || madLibStory[i][0] == ',')
cout << madLibStory[i];
else
cout << " " << madLibStory[i];
}
}
The number appears to be coming from numWords on line 27.
Line 154 - I think you mean the slash to go the other way - '\"'
Spacing - the getPunctuation function is adding in spaces to the words that represent quotation marks. Then the display function adds a space before every word except only the first word and words that represent periods and commas - it doesn't take into account quotation marks.
Edit: When do you expect the "Do you want to play again?" prompt to appear?