MadLib game read file issue

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!

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <fstream>    // used for getting and reading a file. 
using namespace 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;
      }
      else if (yesOrNo == 'y')
         playAgain = true;

      else if (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++;
      }
      else if (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);
   }
   else if(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];
       else if (madLibStory[i][0] == '.' || madLibStory[i][0] == ',')
         cout << madLibStory[i];
      else
         cout << " " << madLibStory[i];
   }

}


Last edited on
Can you post the content of a sample text file that you are using for this?
The text file is in an SSH computer from my school. The inside of the file is this:

 
My pet cat is very <{> <adjective>.  <}> <#> So is my dog . <#> 
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?
Last edited on
Topic archived. No new replies allowed.