MadLib project, text formatting question

I'm having some trouble formatting the text for a homework project.
This project will open a file containing the text for a madlib. It will search the text for special symbols, letting it know to prompt the user for word types (adjective, noun, etc...), it then formats the madlib and display it. Then ask the user if they want to play again.

Here is my code:

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
#include <iostream>
#include <fstream>
using namespace std;

void getFileName(char *fileName);
int readFile(char *fileName, char madLib[][32]);
char promptWordType(char madLib[][32], int numChar);
void getAnswers(char madLib[][32], int i);
void display(char madLib[][32], int numChar);
void playAgain(char fileName[256]);

int main()
{
   char fileName[256];
   getFileName(fileName);

   int numChar = 0;
   char madLib[256][32];
   numChar = readFile(fileName, madLib);
   promptWordType(madLib, numChar);

   display(madLib, numChar);
   playAgain(fileName);

   return 0;
}

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

char promptWordType(char madLib[][32], int numChar)
{
   for (int i = 0; i < numChar; i++)
   {
      if (madLib[i][0] == '<')
      {
         if (isalpha(madLib[i][1]))
         {
            cout << "\t";
            for (int j = 1; madLib[i][j] != '>'; j++)
            {
               if ( j == 1)
                  cout << (char)toupper(madLib[i][1]);
               else if (madLib[i][j] == '_')
                  cout << ' ';
               else
                  cout << madLib[i][j];
            }

            cout << ": ";
            getAnswers(madLib, i);
         }
      }
   }
   cout << endl;
}

void getAnswers(char madLib[][32], int i)
{
   cin.getline(madLib[i], 256);
}

void display(char madLib[][32], int numChar)
{
   for (int i = 0; i < numChar-1; i++)
   {
      if (madLib[i][0] == '<')
      {
         switch (madLib[i][1])
         {
            case '#':
               cout << '\n';
               madLib[i][0] = '\n';
               madLib[i][0] = '\0';
               break;

            case '{':
               cout << '"';
               break;

           case '}':
               cout << '"';
               cout << ' ';
               break;

            case '[':
               cout << '\'';
               break;

            case']':
               cout << '\'';
               cout << ' ';
               break;
            
            case '.':
               cout << endl;
               break;

            case '?':
               cout << endl;
               break;

            case '"':
               cout << " ";
               break;

            case '!':
            break;
         }
      }
      else if (madLib[i + 1][0] == '<' || ispunct(madLib[i+1][0]))
      {
         cout << madLib[i];
         switch (madLib[i+1][1])
            {
               case '{':
               case '[':
                  cout << ' ';
                  break;

               case '#':
               case '}':
               case ']':
               case '.':
               case '?':
               case '!':
                  break;
            }
      }
      else
         cout << madLib[i] << " ";
   }
   cout << endl;
}

void playAgain(char fileName[256])
{
   char choice;
   cout << "Do you want to play again (y/n)? ";
   cin >> choice;

   if(choice == 'y')
   {
      cin.getline(fileName, 256).clear();
      main();
   }
   else
 {
      cout << "Thank you for playing." << endl;
   }
}


Mad Lib sample text file:
1
2
3
4
5
6
7
8
9
   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 ! <#>
 


After an end quotation mark, it will entera space, to separate it and the next word.

The problem is it does this at the end of the text, and it triggers test bed.
I want it to just end the line in this case.

1
2
3
4
5
6
7
8
9
10
11
   > I feel like yelling "Automobile Magazine.com" every time I drive the
   > Internet. So many cars to look for, so many motorcycles to find.
   >
   > My mother asked me, "New York, what is so red about
   > the Web?" "Well," I said, "just the other day I did a search for Porsche
   > by entering 'BMW xor Ferrari' in a search
   > engine. And it returned Car and Driver and Ferrari.it. It's
   > just so fast!" \n
Exp: just so fast!"\n
   >
   > And then she knew what I meant. I felt so happy! 


This is where the error is:
1
2
3
   > just so fast!" \n
Exp: just so fast!"\n
   >


I'm thinking I need to make a special case in my loop in display, that is its an end quotation, and its the end of the text, it will just end the line.

I've tried a couple things but I just can't seem to figure it out, suggestions would be greatly appreciated, thanks.

/* 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]
*/
Last edited on
Topic archived. No new replies allowed.