cstring understanding

Im trying to convert the (push_back) and (.clear()) to a readable cstring. any help would be greatly appreciated.

if you want more information ( http://www.cplusplus.com/forum/beginner/74087/ )

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
void commandsAndPrompts(char madLib[][32])
{
   for (int i = 0; i < (int)madLib.size(); i++)
   {
      if (isBrackets(madLib, i))
      {
         vector<char> madLibCommandReplace;
         switch (madLib[i][1])
         {

            case '#':
               madLibCommandReplace.push_back('\n');
               madLib[i] = madLibCommandReplace;
               break;


            case '{':

               madLibCommandReplace.push_back(' ');
               madLibCommandReplace.push_back('"');
               madLib[i] = madLibCommandReplace;
               break;

            case '}':
               madLibCommandReplace.push_back('"');
               madLib[i] = madLibCommandReplace;
               break;

            case '[':
               madLibCommandReplace.push_back(' ');
               madLibCommandReplace.push_back('\'');
               madLib[i] = madLibCommandReplace;
               break;

            case ']':
               madLibCommandReplace.push_back('\'');
               madLib[i] = madLibCommandReplace;
               break;

            default:
               getMadLibPrompt(madLib[i]);
               break;
         }
      }
   }
}



my full code is here is this helps. you'll also notice that I started to convert the whole program over, but I have not finished doing so. just a little confused on the function previously stated above.

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#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 wordNum) //int vector level
{
   return (madLib[wordNum][0] == '<' &&
           madLib[wordNum][strlen(madLib[wordNum]) - 1] == '>');
}

/*************************************************************
 *
 *
 **************************************************************/
void changeFormat(char madLib, char prompt)
{
   for (int i = 0; i < madLibPrompt[256]; i++)
   {
      char currentChar = madLibPrompt[i];
      if (i == 0 || i == (int)madLibPrompt.size() - 1);
      else if (i == 1)
      {
         currentChar -= 32;
         prompt.push_back(currentChar);
      }
      else if (currentChar == '_')
         prompt.push_back(' ');
      else
         prompt.push_back(currentChar);
   }
}

/*************************************************************
 *
 *
 **************************************************************/
void getMadLibPrompt(vector < char > &madLib)
{

   vector < char > prompt;
   char answer[256];

   changeFormat(madLib, prompt);


   cout << " \t";
   for (int dis = 0; dis < (int)prompt.size(); dis++) //prompt[]
      cout << prompt[dis];
   cout << ": ";

   cin.getline(answer, 256);
   madLib.clear();
   for (int i = 0; answer[i] != '\0'; i++)
      madLib.push_back(answer[i]);
   //next question
   //
}

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

            case '#':
               madLibCommandReplace.push_back('\n');
               madLib[i] = madLibCommandReplace;
               break;


            case '{':

               madLibCommandReplace.push_back(' ');
               madLibCommandReplace.push_back('"');
               madLib[i] = madLibCommandReplace;
               break;

            case '}':
               madLibCommandReplace.push_back('"');
               madLib[i] = madLibCommandReplace;
               break;

            case '[':
               madLibCommandReplace.push_back(' ');
               madLibCommandReplace.push_back('\'');
               madLib[i] = madLibCommandReplace;
               break;

            case ']':
               madLibCommandReplace.push_back('\'');
               madLib[i] = madLibCommandReplace;
               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])
{
   cout << endl;
   for (int i = 0; i < char madLib[][32]; i++)
      for (int dis = 0; dis < char madLib[][32]; dis++)
         cout << madLib[i][dis];
}

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


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


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

      char question[] = "Do you want to play again (y/n)? ";
      cout << question;
      cin >> ans;
   }
   while (ans == 'y');
   cout << "Thanks for playing.\n";
   cin.get();
   return 0;
}


Last edited on
std::string also has push_back() and clear(). If you want to get a c string out of a std::string you can use the c_str() function.
thanks but I cant use #include <string>. Im pretty much stuck with what you see in the code already.

1
2
3
4
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring> 
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
void commandsAndPrompts(char (*madlib)[32], int numWords)
{
   for (int i = 0; i < 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;
         }
      }
   }
}
hey thanks that helped a lot! almost done with everything, now im trying to fix the setSpaces() function. once I get that I should be on my way, with maybe a few little tweaks here and there.

here's what I got 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
Im soo close. I just edited my last post so that it is up to date. please help me.
line 124: char madLib[][32] is nonsense. Provide the count
1
2
3
4
void setSpaces(char madLib[][32], int count)
{
   //   vector <char> quoteAndTickPushBack;
   for (int i = 0; i < count; i++)
and your're done

line 149 is dangerous because you might access elments out of bounds. The same applies to the lines 132,136,139,143
Topic archived. No new replies allowed.