Run-Time Check Failure #3 quiz game

Hey, new here.

I'm trying (unsuccessfully) to make a quiz game
Run-Time Check Failure #3 - The variable 'x' is being used without being initialized."
what is this error taking place, thanks! :D

here's 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
155
156
157
158
159
160
161
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int x;
char ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10, ch11, ch12, ch13;
cout<<"Welcome to Quiz Bee Game!\n";
cout<<"\nEnter name: ";
getline (cin, name);
cout<<"\nHello " <<name <<"! This is the First Round.\n \nGoal: Get 30 - 15 points to proceed to the next round. Correct answer: 5 points. Wrong answer: 2 points.\n";
cout<<"\n————————————————————————\n";
cout<<"\nMultiple choice: Enter answer in form of 'a', 'b' and 'c' only. \n";
cout<<"\n1.) Who designed the C++?\n \na. Bjarne Stroustrup \nb. Barne Strostrup \nc. Barney Straptrap \nAnswer: ";
cin>>ch1;
if (ch1 == 'a' || ch1 == 'A')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch1 == 'b' || ch1 == 'B')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'A'. Your score is: " <<x <<"\n";
}
else if (ch1 == 'c' || ch1 == 'C')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'A'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";

cout<<"\n2.) Who is the first programmer?\n \na. Adam Lovelace \nb. Ada Lovelace \nc. Ada Lovebug \nAnswer: ";
cin>>ch2;
if (ch2 == 'b' || ch2 == 'B')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch2 == 'a' || ch2 == 'A')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'B'. Your score is: " <<x <<"\n";
}
else if (ch2 == 'c' || ch2 == 'C')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'B'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";
cout<<"\n3.) What is an int?\n \na. Float \nb. Integer \nc. Double \nAnswer: ";
cin>>ch3;
if (ch3 == 'b' || ch3 == 'B')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch3 == 'a' || ch3 == 'A')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'B'. Your score is: " <<x <<"\n";
}
else if (ch3 == 'c' || ch3 == 'C')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'B'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";

cout<<"\n4.) What are the Boolean operators?\n \na. true and false \nb. yes and no \nc. left and right \nAnswer: ";
cin>>ch4;
if (ch4 == 'a' || ch4 == 'A')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch4 == 'b' || ch4 == 'B')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'A'. Your score is: " <<x <<"\n";
}
else if (ch4 == 'c' || ch4 == 'C')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'A'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";
cout<<"\n5.) What is the sign for Increment operator?\n \na. == \nb. -- \nc. ++ \nAnswer: ";
cin>>ch5;
if (ch5 == 'c' || ch5 == 'C')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch5 == 'a' || ch5 == 'A')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
else if (ch5 == 'b' || ch5 == 'B')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";
cout<<"\n6.) The sign for logical OR operator is? \na. && \nb. == \nc. || \nAnswer: ";
cin>>ch6;
if (ch6 == 'c' || ch6 == 'C')
{
x = x + 5;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch6 == 'a' || ch6 == 'A')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
else if (ch6 == 'b' || ch6 == 'B')
{
x = x + 2;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";

if (x == 30 || x == 29 || x == 28 || x == 27 || x == 26 || x == 25 || x == 24 || x == 23 || x == 22 || x == 21 || x == 20 || x == 19 || x== 18 || x == 17 || x == 16 || x == 15){
cout<<"\nCongratulations! \nThis is the 'Final LEVEL'.\n \nGoal: Get 100 - 70 points. Correct answer: 10 points. Wrong answer: 4 points.\n";
cout<<"\n————————————————————————\n";
cout<<"\nMultiple choice: Enter answer in form of 'a', 'b' and 'c' only. \n";
cout<<"\n1.) The sign for Arithmetic operator Modulo is? \na. + \nb. * \nc. % \nAnswer: ";
cin>>ch7;
if (ch7 == 'c' || ch7 == 'C'){
x = x + 10;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch7 == 'a' || ch7 == 'A')
{
x = x + 4;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
else if (ch7 == 'b' || ch7 == 'B')
{
x = x + 4;
cout<<"Sorry, wrong answer, the answer is 'C'. Your score is: " <<x <<"\n";
}
cout<<"\n————————————————————————\n";

cout<<"\n2.) The sign that ends the statements? \na. : \nb. ; \nc. { } \nAnswer: ";
cin>>ch8;
if (ch8 == 'b' || ch8 == 'B')
{
x = x + 10;
cout<<"Good job. Your score is: " <<x <<"\n";
}
else if (ch8 == 'a' || ch8 == 'A')
{
x = x + 4;
cout<<"Sorry, wrong answer, the answer is 'B'. Your score is: " <<x <<"\n";
}
else if (ch8 == 'c' || ch8 == 'C')
{


Sorry coudnlt post the complete code, too long.
not really sure, ill look into it, im just reading guides and stuff online just trying to learn by myself. thanks for the reply appreciate it. here's the full code...

http://txt.do/58wh1
Last edited on
kinda like quiz bee, those are for keeping score counts
You can replace this:
1
2
3
    if (x == 30 || x == 29 || x == 28 || x == 27 || x == 26 || x == 25 || x == 24
        || x == 23 || x == 22 || x == 21 || x == 20 || x == 19 || x == 18 || x == 17
        || x == 16 || x == 15) {

with this:
if (15 <= x && x <=30) {

Have you learned about functions? You can save a lot of repeated code by using a function to ask the question, read the user's answer, and process it:

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

// Ask the "question" and prompt for a guess.
// If the guess is the same as "answer" then add 5 to "score", otherwise add 2.
void ask(const char *question,
	 char answer,
	 int &score)
{
    char guess;
    cout << '\n' << question << '\n';
    cout << "Answer: ";
    
    cin >> guess;
    if (toupper(guess) == answer) {
	score += 5;		// same as score = score + 5;
	cout << "Good job. Your score is: " << score << "\n";
    } else {
	score += 2;
	cout << "Sorry, wrong answer, the answer is '" << answer
	     << "'. Your score is: " << score << "\n";
    }
    cout << "\n————————————————————————\n";
}

int
main()
{
    string name;
    int x=0;

    cout << "Welcome to Quiz Bee Game!\n";
    cout << "\nEnter name: ";
    getline(cin, name);
    cout << "\nHello " << name <<
	"! This is the First Round.\n \nGoal: Get 30 - 15 points to proceed to the next round. Correct answer: 5 points. Wrong answer: 2 points.\n";
    cout <<
	"\n————————————————————————\n";
    cout << "\nMultiple choice: Enter answer in form of 'a', 'b' and 'c' only. \n";
    ask("\n1.) Who designed the C++?\n \na. Bjarne Stroustrup \nb. Barne Strostrup \nc. Barney Straptrap ",
	'A', x);

    ask("2.) Who is the first programmer?\n \na. Adam Lovelace \nb. Ada Lovelace \nc. Ada Lovebug ", 'B', x);
    ask("3.) What is an int?\n \na. Float \nb. Integer \nc. Double ", 'B',x);
    ask("4.) What are the Boolean operators?\n \na. true and false \nb. yes and no \nc. left and right ", 'A', x);
    ask("5.) What is the sign for Increment operator?\n \na. == \nb. -- \nc. ++ ", 'C', x);
    ask("6.) The sign for logical OR operator is? \na. && \nb. == \nc. || ", 'C', x);

    if (15 <= x && x <= 30) {
	cout <<
	    "\nCongratulations! \nThis is the 'Final LEVEL'.\n \nGoal: Get 100 - 70 points. Correct answer: 10 points. Wrong answer: 4 points.\n";
	cout <<
	    "\n————————————————————————\n";
	cout << "\nMultiple choice: Enter answer in form of 'a', 'b' and 'c' only. \n";
	ask("1.) The sign for Arithmetic operator Modulo is? \na. + \nb. * \nc. % ", 'C', x);
	ask("2.) The sign that ends the statements? \na. : \nb. ; \nc. { } ", 'B',x);
	ask("3.) The one and only function of c++? \na. int main () \nb. using namespace std; \nc. #include<iostream> ", 'A', x);
	ask("4. Surround the body of the function, which may have one or more instructions or statements. \na. { } \nb. ( ) \nc. # ", 'A', x);
	ask("5.) Preprocessor command which tells compiler. \na. using namespace std; \nb. #include <iostream> \nc. int main () ", 'B', x);
	ask("6. The value zero to the Operating system. \na. return 0; \nb. int main () \nc. using namespace std; ", 'A', x);
	ask("7.) It is a library function that is used to print data on the user screen. \na. std \nb. cout \nc. cin ", 'B', x);
	if (65 <= x && x <= 100) {
	    cout << "\nCongratulations, " << name << " YOU WIN! \nYour score is: " << x <<
		"/100";
	} else if (31 <= x && x <= 64) {
	    cout << "\nGame over! YOU LOSE!";
	}
    } else if (0 <= x && x <= 15) {
	cout << "\nGame over! Better luck next time!";
    }
    return 0;
}

@gentleguy

wow yea its clean and easy to understand, thanks! ill try not to complicate it next time :)

@dhayden
thanks for feedback ill need to practice more functions

If you pay attention more closely to the OP questions, you would realise not every question gives the 'player' five points if answered correctly.

Well, the original question was about an uninitialized variable, not the number of points for each question. Still, you are correct that I missed the difference in points for each question.

The way to handle this is to pass the point values as parameters to the ask() function. Easy peasy.
Topic archived. No new replies allowed.