trying to clean up the code, how to....

thank you to all those who have helped up to this point. so i've almost got it all converted from <Vector> to a readable <cstring>. But I ran into a wall when I got to the setSpaces() function. any help would be appreciated. thanks much.

to better explain myself, I've been doing this. changing this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void readMadLib(char fileName[], char madLib[][32])
{
	ifstream inStream(fileName);
	
	while (inStream.fail())
	{
		getFileName(fileName);
		inStream.open(fileName);
	}
	
	char inputChar[256];
	
	while (inStream >> inputChar)
	{
	vector < char > word;  //this stores words
		
		for (char *p = inputChar; *p; p++)
			word.push_back(*p);
		madLib.push_back(word);  //sends word to next spot
	}	
	inStream.close();
}


to this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int readMadLib(char fileName[], char madLib[][32])
{
   ifstream inStream(fileName);

   if (!inStream.fail())
   {
      int i = 0;
      while (inStream >> madLib[i])
      {
         i++;
      }
      inStream.close();
      return i;
   }
   return 0;
}



He is the code I have so far..
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;

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

/*************************************************************
 *
 *
 **************************************************************/
int readMadLib(char fileName[], char madLib[][32])
{
   ifstream inStream(fileName);

   if (!inStream.fail())
   {
      int i = 0;
      while (inStream >> madLib[i])
      {
         i++;
      }
      inStream.close();
      return i;
   }
   return 0;
}

/*************************************************************
 *
 *
 **************************************************************/
bool isBrackets(char madLib[][32], int numWords)
{
   return (madLib[numWords][0] == '<' &&
           madLib[numWords][strlen(madLib[numWords]) - 1] == '>');
}

/*************************************************************
 *
 *
 **************************************************************/
void getMadLibPrompt(char madLib[][32], int i)
{

   char prompt[256];


    for(int j = 0; j < 32; j++)
  {
     if (madLib[i][j] == '_')
        madLib[i][j] = ' ';

     else if (madLib[i][j] == '>')
        madLib[i][j] = '\0';
  }
   madLib[i][1] = toupper(madLib[i][1]);

   for(int j = 0;j < 32; j++)
   {
      prompt[j] = madLib[i][j + 1];
   }

    cout << " \t";
    cout << prompt;
    cout << ": ";
    cin.getline(madLib[i], 256);


}

/*************************************************************
 *
 *
 **************************************************************/
void commandsAndPrompts(char madLib[][32], int numWords)
{
   for (int i = 0; i < (int)numWords; i++)
   {
      if (isBrackets(madLib, i))
      {
         switch (madLib[i][1])
         {

            case '#':
               strcpy(madLib[i], "\n");
               break;


            case '{':
               strcpy(madLib[i], " \"");
               break;

            case '}':
               strcpy(madLib[i], "\"");
               break;

            case '[':
               strcpy(madLib[i], " \'");
               break;

            case ']':
               strcpy(madLib[i], "\'");
               break;

            default:
               getMadLibPrompt(madLib, i);
               break;
         }
      }
   }
}

/*************************************************************
 *
 *
 **************************************************************/
void setSpaces(char madLib[][32])
{
   //   vector <char> quoteAndTickPushBack;
   for (int i = 0; i < char madLib[][32]; i++)
   {
      if (madLib[i][0] == '\n');
      else if (madLib[i][1] == '"' || madLib[i][1] == '\'')
      {
         if (madLib[i - 1][0] == '"')
         {
            quoteAndTickPushBack.clear();
            quoteAndTickPushBack.push_back('"');
            madLib[i - 1] = quoteAndTickPushBack;
         }

         else if (madLib[i - 1][0] == '\'')
         {
            quoteAndTickPushBack.clear();
            quoteAndTickPushBack.push_back('\'');
            madLib[i - 1] = quoteAndTickPushBack;
         }
      }

      else
      {
         switch((madLib[i + 1][0]))
         {
            case '\n':
            case '"':
            case '!':
            case ' ':
            case '\'':
            case ')':
            case ',':
            case '.':
            case ';':
            case '?':
            case ']':
            case '`':
            case '}':
               break;
            default:
               madLib[i].push_back(' ');
         }
      }
   }
}
/*************************************************************
 *
 *
 **************************************************************/
void display(char madLib[][32],int numWords)
{
   cout << endl;
   for (int i = 0; i < numWords; i++)

         cout << madLib[i];
}

/*************************************************************
 *
 *
 **************************************************************/
int main()
{
   char ans;

      do
      {
         char madLib[1024][32];
         char fileName[256];


         getFileName(fileName);    //good
         int numWords = readMadLib(fileName, madLib); //good
         commandsAndPrompts(madLib, numWords);
         //        setSpaces(madLib);
          display(madLib, numWords);

         char question[] = "Do you want to play again (y/n)? ";
         cout << question;
         cin >> ans;
         cin.ignore();
      }
      while (ans == 'y');
      cout << "Thanks for playing.\n";
      return 0;
}
Last edited on
o-and if you see anything else that seems out of line, please let me know.

This is what the code should do.

takes a .txt file

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 beside 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 . <#>

# = Newline character. No space before or after.
{ = Open double quotes. Space before but not after.
} = Close double quotes. Space after but not before.
[ = Open single quote. Space before but not after.
] = Close single quote. Space after but not before.
anything else = A prompt

Last edited on
Topic archived. No new replies allowed.