Quiz Help

I am making a c++ program that is a 6 question true or false quiz and my coding is this 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
#include<iostream>
#include<string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
    
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'True' or 'False'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;
while (char1 != "True" || "False")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
}



  system("PAUSE");
  return 0;

}


But when I input the True or False it keeps displaying an incorrect answer message
Okay so I fixed part of the problem

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
#include<iostream>
#include<string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
    
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'T' or 'F'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;


if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer the answer is False"<<endl;
};


cout<<"No#2 The Squareroot of 3136 is 56?"<<endl;      


  system("PAUSE");
  return 0;

}


My problem is that when i give it the correct answer the first time it wont cut straight to the other part of the code it will display you entered an incorrect character stuff until the two times is over.
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
#include <iostream>
using namespace std;

int main()
{
	char q1;
	char q2;
    
	cout << "Hello, this is a standard TRUE or FALSE quiz.\n\n";
	cout << "#1. George Washington invented the toilet.\n";
	cout << "Please enter ""T"" or ""F""\n";
	cin >> q1;

	if ((q1 == 'T') || (q1 == 't'))
	{
		cout << "You entered the INCORRECT answer.\n\n";
	}
	else if ((q1 == 'F') || (q1 == 'f'))
	{
		cout << "You entered the CORRECT answer!\n\n";
	}
	else
	{
		cout << "ERROR: Invalid input.\n\n";
	}

	cout << "#2. The square root of 3,136 is 56.\n";
	cout << "Please enter ""T"" or ""F""\n";
	cin >> q2;
	// ... finish code

	system("PAUSE");
	return 0;
}

Since it is a true or false quiz, you don't have to worry about catching separate input for:
TRUE, T, t, FALSE, F, and f.

We are only worried about whether it is True, False, or Invalid.
Use a char instead of a string, this way when the user enters "TRUE" or "FALSE", it is held as "T" or "F"

The char data type can only hold a single letter, meaning it will only read the first letter of
the word "TRUE" or "FALSE" - input as: T or F (or given the invalid input error)
Last edited on
the while condition should be written like this
while (char1 != "True" && char1 !="False")
Last edited on
Here is the finished product with the same problem

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include<iostream>
#include<string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
    
cout<<"Hello this is your standard True and False quiz"<<endl;
cout<<"Please enter 'T' or 'F'"<<endl;
cout<<"No#1 George Washington invented the toilet?"<<endl;
cin>>char1;


if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False"<<endl;
};



cout<<"No#2 The Squareroot of 3136 is 56?"<<endl;      
cin>>char2;
if ( char2 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char2;
};
if ( char2 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True"<<endl;
};



cout<<"No#3 Do you capitalize the seasons?"<<endl;      
cin>>char3;
if ( char3 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char3;
};

if ( char3 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False"<<endl;
};



cout<<"No#4 Is a drum in the percussion area?"<<endl;      
cin>>char4;
if ( char4 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char4;
};

if ( char4 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True"<<endl;
};



cout<<"No#5 The Egg came before the Chicken?"<<endl;      
cin>>char5;
if ( char5 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char5;
};

if ( char5 == "F" || "T" )
{
cout<<"Ha! trick question the answer depends on if you a "<<endl;
cout<<"Creationist or an Evolutionist "<<endl;
};



cout<<"No#6 Computers help everyday life?"<<endl;      
cin>>char6;
if ( char6 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char6;
};

if ( char6 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True"<<endl;
};



cout<<"Your answers where"<<endl;
cout<<""<<endl;
cout<<char1<<char2<<char3<<char4<<char6<<endl;
cout<<""<<endl;


if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 0 correct and 5 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect"<<endl;
};

if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 1 correct and 4 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};

if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 4 correct and 1 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect"<<endl;
};


if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect"<<endl;
};


if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect"<<endl;
};


if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 5 correct and 0 incorrect"<<endl;
};









  system("PAUSE");
  return 0;

}
Hello Shadow Kurimeki,

First off: Here is just an example starting on line 14 of your code ...

1
2
3
4
5
if ( char1 != "T" || "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};


If I read your problem correctly, you are getting a message saying that "You entered the incorrect character please reenter True or False" no matter what you enter for each question.

If this is the case, the reason is because the condition (char1 != "T" || "F") in your if statements actually return true every time. The reason for this is that while in english the statement reads, "If char1 is not equal to T or F do the following ... ", in c++ it actually reads like this: "If char1 is not equal to "T" OR IF "F""

This means that in an ||(or) statement, c++ will evaluate both sides of a || and return true if either side is true. Here is the equivalent of your code and maybe it will give you a better understanding:

(char1!="T") || ("F")

Here is what your code should look like to implement what your trying to do:

1
2
3
4
5
if ( char1 != "T" && char1 != "F")
{
cout<<"You entered an incorrect character please reenter True of False"<<endl;
cin>>char1;
};


This evaluates to, "if char1 does not equal T and char1 does not equal F ..."


Also, another note that I think you would really like to incorporate into your program to make it possible to add questions in the future without needing to do another hundred lines of code:

Instead of using an if statement for every possible choice, try implementing this little bit of pseudocode:

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
int correct = 0;
int incorrect = 0;
string char1, char2, char3, char4, char5, char6;

//Question 1
//Get char1
if(answer is correct)
  correct++; //correct = correct + 1;
else //answer incorrect
  incorrect++; //incorrect = incorrect + 1;

//Question 2
//Get char2
if(answer is correct)
  correct++; //correct = correct + 1;
else //answer incorrect
  incorrect++; //incorrect = incorrect + 1;

//Question 3
//etc ...


//Let the user know how he did on the quiz
cout<<"You answered "<<correct<<" correct and "<<incorrect<<"incorrect."<<endl;



Also, one last note. If you are interested in input validation (Making sure the person typed in T or F), read up on how do:whiles work. It makes doing input validation very easy.


Let us know how that works out, any other questions by what I've said just ask.
Last edited on
Topic archived. No new replies allowed.