Writing a mad lib program, need some help

I'm working on a project for my intro c++ class in which we are printing out a mad lib. I am pretty close to finishing it up, but I'm dealing with a couple major bugs right now that I'm not sure how to go about fixing.

First, I'm getting some spaces that I don't want. For example, it will print something out like "Zoos are places where wild <plural_noun> are kept in pens or cages so that <plural_noun> can come and look at them . " ...

Note the weird spacing with the periods and commas (also with the single quotation marks, which thats also a bug, there's conditions where it should be double quotations, but I'm not quite there for figuring it out yet, I really want to fix the spacing issue first).

http://i52.photobucket.com/albums/g23/I_eat_corn/project3bugs.png

madLibZoo.txt:
1
2
3
4
5
6
7
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 besides 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 . <#> 



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
  #include <iostream>
#include <fstream>
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);

int main()
{
   char madLibStory[256][32];
   int numWords;

   char yesOrNo;
   bool playAgain = true;

   while (playAgain)
   {
      numWords = readFile(madLibStory);
      cout << numWords << endl; //Take me out eventually
      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;
         }
      
   }
   
}

void getFileName(char fileName[])
{
   cout << "Please enter the filename of the Mad Lib: ";
   cin >> fileName;
}

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;
   
}

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;
}

void getPunctuation(char punc[])
{   

   {
      switch (punc[1])
      {
         case '#':
            punc[0] = '\n';
            punc[1] = '\0';
            break;
         case '{':
            punc[0] = ' ';
            punc[1] = '\"';
            punc[2] = '\0';
         case '}':
            punc[0] = '\"';
            punc[1] = ' ';
            punc[2] = '\0';
         case '[':
            punc[0] = ' ';
            punc[1] = '\'';
            punc[2] = '\0';
         case ']':
            punc[0] = '\'';
            punc[1] = ' ';
            punc[2] = '\0';
      }
         
   }
   
   return; 
}

void display (char madLibStory[][32], int numWords)   
{
   for (int i = 0; i < numWords; i++)
   {
      if (i == 0)
         cout << madLibStory[i];
      else if (madLibStory[i] == "." || madLibStory[i] == ",")
         cout << madLibStory[i];
      else
         cout << " " << madLibStory[i];
   }

}

/*  No more than 1024 characters total in file
 *  No more than 32 lines in the file
 *  Each line has no more than 80  characters
 *  There are no more than 256 words in the file
 *  Each word is no more than 32 characters in length
 *  madLibStory[word][character]
 */


I have tried several variations within my display function, but it doesnt seem like I can get rid of these unnecessary spaces. The ONLY way I got them to not space is when I just had my display loop as:

1
2
for (int i = 0; i < numWords; i++)
   cout << madLibStory[i];


Obviously, something is wrong with the particular piece of code in the display function because the mistake is only happening when I'm including spaces in the cout's.

I'm not quite sure how to handle the event in which madLibStory[i] is a comma or a period. Also, the spacing appears to be happening with the new line indicator and quotation marks (which are printing out as single quotes and not double as they should in my mabLibZoo.txt file.

I could use some help with this if anyone has an ideas
Last edited on
Since madLibStory[i] is not std::string, but an array, it cannot be equal (in sense of using == operator) to "." or "," - it barely holds the address of first element in the char array.
Change the display function like this, and also cover for apostrophes' case.
1
2
 else if (madLibStory[i][0] == '.' || madLibStory[i][0] == ',')
         cout << madLibStory[i];


EDIT: The other bug is that you don't break your cases in getPunctuation().
Last edited on
Topic archived. No new replies allowed.