Syntax error that I cannot find.

This program prints the same question after the user selects to play again and sometimes it'll print "wrong" even when the user is correct. Im assuming its just a syntax error but Im not sure.

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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;


void convertToLower(string &tempString);




int main ()
{
const int SIZE = 10;
char choice;
unsigned int seed= (unsigned int) (time(0));
srand(seed);

string Questions[SIZE]= {" What fruit in 'The Godfather' appeared before every death? ", "Which country invented ice cream? ", "Which U.S state is known for peaches? ", 
"What is the common name for dried plums? ",  "What kind of alchohol is Russia known for? ", "What is the rarest M&M color? ", "Which is the only edible food that never goes bad? ",
 "Which country consumes the most chocolate per capita? ","What is the most consumed manufactured drink in the world? ", "Which state would you eat laulau in? "};

string Answers[SIZE]={"oranges", "china", "georgia", "prunes", "vodka", "brown", "honey", "switzerland", "tea", " hawaii"};

int generator = rand()%10 +1 ;

string answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9,answer10;




cout<<endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<< "          Welcome to Foood Trivia!!!"<<endl;
cout<<endl;
cout<<"Ready to Play? (Y/N) ";
cin>>choice;
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

while ((choice == 'Y' ) || (choice=='y')) 
{
    cout << Questions[generator]; 
    
 if ((choice =='N') || (choice =='n'))
 {
     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
     <<endl
     << "Maybe next time!\n "
     <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
     <<endl;
     exit(-1);
 }





if (Questions[generator] == Questions[0])
{
cin>>answer1;
convertToLower(answer1);

if (answer1==Answers[0])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
}
  else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}




if (Questions[generator] == Questions[1])
{
cin>>answer2;
convertToLower(answer2);

if (answer1==Answers[1])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
 }
  else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}




if (Questions[generator] == Questions[2])
{
cin>>answer3;
convertToLower(answer3);

if (answer3==Answers[2])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
    
 }
  else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}



if (Questions[generator] == Questions[3])
{
cin>>answer4;
convertToLower(answer4);

if (answer4==Answers[3])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
 }
 else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}




if (Questions[generator] == Questions[4])
{
cin>>answer5;
convertToLower(answer5);

if (answer5==Answers[4])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
}
else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}


if (Questions[generator] == Questions[5])
{
cin>>answer6;
convertToLower(answer6);

if (answer6==Answers[5])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
    
}else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}



if (Questions[generator] == Questions[6])
{
cin>>answer7;
convertToLower(answer7);

if (answer7==Answers[6])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
}else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}





if (Questions[generator] == Questions[7])
{
cin>>answer8;
convertToLower(answer8);

if (answer8==Answers[7])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
    
}else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}

  



if (Questions[generator] == Questions[8])
{
cin>>answer9;
convertToLower(answer9);


if (answer9==Answers[8])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
    
}else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}




if (Questions[generator] == Questions[9])
{
cin>>answer10;
convertToLower(answer10);

if (answer10==Answers[9])
{
    cout<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl; 
 }else{
    cout<<endl;
    cout<<"WRONG!\n";} 
    cout<<endl;
}



 
    cout<< "Play again? (Y/N)?   ";
    cin>>choice;


}




return 0;

}




void convertToLower(string &tempString)  
{

unsigned int i; 
for (i = 0; i < tempString.length(); i++) 
tempString[i] = tolower(tempString[i]); 
 
tempString[i] = '\0'; 
} 
Last edited on
Your generator variable is being set outside of your main loop. Try putting generator = rand %10; after line 46. Don't add one, you want it to include 0 as an option for your array index.
Also you do have a copy-pasted syntax error, see lines 76, 96, 117, etc.

Your arrays are set up in a way that you could use a shortcut in your 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

void convertToLower(string &tempString);

int main ()
{
	const int SIZE = 10;
	char choice;
	unsigned int seed= (unsigned int) (time(0));
	srand(seed);

	string Questions[SIZE]= {" What fruit in 'The Godfather' appeared before every death? ", "Which country invented ice cream? ", "Which U.S state is known for peaches? ",
	"What is the common name for dried plums? ",  "What kind of alchohol is Russia known for? ", "What is the rarest M&M color? ", "Which is the only edible food that never goes bad? ",
	 "Which country consumes the most chocolate per capita? ","What is the most consumed manufactured drink in the world? ", "Which state would you eat laulau in? "};

	string Answers[SIZE]={"oranges", "china", "georgia", "prunes", "vodka", "brown", "honey", "switzerland", "tea", "hawaii"};

	int generator; // this gets set at the top of the loop

	string userAnswer;

	cout<<endl;
	cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
	cout<< "          Welcome to Foood Trivia!!!"<<endl;
	cout<<endl;
	cout<<"Ready to Play? (Y/N) ";
	cin>>choice;
	cout<<endl;
	cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

	while ((choice == 'Y' ) || (choice=='y')) 
	{
		generator = rand()%10;
		cout << Questions[generator]; 
	
		cin>>userAnswer;
		convertToLower(userAnswer);
		if (userAnswer==Answers[generator])
		{
			cout<<endl;
			cout<<"Correct!"<<endl;
			cout<<endl; 
		}
		else
		{
			cout<<endl;
			cout<<"WRONG!\n";
			cout<<endl;
		}
	
		cout<< "Play again? (Y/N)?   ";
		cin>>choice;
	}

	if ((choice =='N') || (choice =='n'))
	{
		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
		<<endl
		<< "Maybe next time!\n "
		<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
		<<endl;
	}


	return 0;
}

void convertToLower(string &tempString)  
{
	unsigned int i;
	for (i = 0; i < tempString.length(); i++) 
	tempString[i] = tolower(tempString[i]);
	tempString[i] = '\0'; 
} 

Your while loop condition says that choice must be y or Y in order for it to run again, so your exit statement would never run. I moved it to the end of the program, but it could also have been placed at the end of the loop just after choice is provided by the user...
The last thing I caught is that your final answer " hawaii" has a space in front which means answering "hawaii" will still be wrong. just remove that extra space.
Fun program, my suggestion for moving foreward is to set it up to read questions and answers from a file so that they can be changed without compiling the whole program.
Nice work.
Last edited on
You've actually helped me so much, thank you.
Glad I could help.
vasncode, you should remove line 284 (77 in newbieg's post). It's entirely redundant (and before C++11 was possibly undefined behavior).

https://en.cppreference.com/w/cpp/string/basic_string/operator_at
If pos == size(), a reference to the character with value CharT() (the null character) is returned.
For the first (non-const) version, the behavior is undefined if this character is modified to any value other than CharT() .
(since C++11)


And indent the statement in the loop.
Topic archived. No new replies allowed.