Variable being used without being initialized?

Hey, everyone. I'm having a problem with a code I'm working on. I just started learning C++ about two weeks ago, so bear with the potentially messy and unconventional way of my coding.

The program is basically a math sheet where you select the difficulty and amount of questions, and you answer randomly generated questions, and it scores you in the end.

After the first question's answer is inputted, I get a "Debug Error", and it says: "Run-Time Check Failure #3 - The variable 'totalscore' is being used without being initialized."

Here's a snippet of 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
// Math Sheet
cout << "o------------------------------o\n";
cout << "|        2. Math Sheet         |\n";
cout << "o------------------------------o\n\n";

// Difficulty
cout << "Select difficulty (or type 0 to go\n";
cout << "back to the menu):\n";
cout << "\t1. Easy (0-20)\n";
cout << "\t2. Medium (0-100)\n";
cout << "\t3. Hard (0-300)\n";
cout << "\t4. Extreme (0-1,000)\n\n";

int mathSheetDifficulty, difficulty;
cin >> mathSheetDifficulty;

if (mathSheetDifficulty == 1) {
	difficulty = 20;
} else if (mathSheetDifficulty == 2) {
	difficulty = 100;
} else if (mathSheetDifficulty == 3) {
	difficulty = 300;
} else if (mathSheetDifficulty == 4) {
	difficulty = 1000;
} else if (mathSheetDifficulty == 0) {
	system("cls");
	goto themenu;
}

// Question Amount
int question(1), questionAmount;
cout << "\nType in the of questions you'd like\n";
cout << "(or type 0 to go back to the menu):\n";
cin >> questionAmount;

if (questionAmount == 0) {
	system("cls");
	goto themenu;
}

int score(0), totalscore;

// Problem
while (question < questionAmount) {
	// Total score & Random seed
	srand(time(0));

	cout << "\nQuestion " << question << " / " << questionAmount << endl;
	int nan;
	int a = rand() % difficulty + 1;
	int b = rand() % difficulty + 1;
	cout << "\t" << a << " + " << b << " = ?\n";
	cin >> nan;

	if (nan == a + b)
	{
		cout << "\n\tCorrect!\n"; // Correct
		score += 1;
		cout << "Your current score: " << score << " / " << totalscore << "\n\n";
	} else {
		cout << "\n\tWrong!\n"; // Wrong
		cout << "Your current score: " << score << " / " << totalscore << "\n\n";
	}
}

cout << "[ Your total score is: " << score << " / " << totalscore << " ]\n\n";
cout << "Press any key to go back to the menu.\n";
getch(); // Press any key to continue, ("#include <conio.h>" required) 
system("cls"); // clear
goto themenu;


I can kind of tell where the problem is. It has to be under the "// Problem" header; something to do with the while code and the variables that are in them, that should be out of them or something. I've messed around with it a bit and couldn't figure it out.

Thanks ahead!
Last edited on
You only need to seed the random number generator once. Just throw it to the top of the main function.

1
2
3
int main(void) {  
       srand(time(0));
       return 0;

I really hate to comment on the code members post, but I feel like I need to here.

1
2
system("cls"); // clear
goto themenu;

Those two lines are both evil, and I mean just terrible to have in your program. Calling system is very resourceful and leaves a huge security hole in your program. Goto statements and labels are terrible practice and make your code incredibly difficult to understand. Most of the time goto statements and labels can be replaced with loops or functions.

Why System is Evil: http://www.cplusplus.com/forum/articles/11153/
Last edited on
The variable 'totalscore' is being used without being initialized."


The problem is exactly that - you have not set totalscore equal to any thing then tried to print it out.

If you get into the practice of initialising variables (even to 0) when you declare them (or in a constructor if using classes), then you won't have this problem - just the answer will be wrong (0 say).
Alright, so the srand goes out of the loop, got that.

As far as the system/goto commands, I know they're bad. But, for now since I'm only a beginner and this isn't an official project or anything, just something I'm messing around with for me, I'll keep it. But eventually, I'll rework the code to not include those commands. Thanks!

Ah, I get it! So total score is the exact same as questionAmount. But I forgot why I needed to create another variable for questionAmount. I remember I was seriously doing some hard thinking about that, and there was a reason why I needed two variables for the same amount (or maybe there isn't a reason).
So I'll just make totalscore equal the questionAmount by doing:
1
2
3
4
5
6
7
8
9
10
11
12
// Question Amount
int question(1), questionAmount;
cout << "\nType in the of questions you'd like\n";
cout << "(or type 0 to go back to the menu):\n";
cin >> questionAmount;

if (questionAmount == 0) {
	system("cls");
	goto themenu;
}

int score(0), totalscore(questionAmount);


I also noticed my question number wasn't going up, was stuck at question 1, so I added this:
1
2
3
4
5
6
7
8
9
10
11
if (nan == a + b)
{
	cout << "\n\tCorrect!\n"; // Correct
	score += 1; // + 1 score
	question += 1; // + 1 question amount
	cout << "Your current score: " << score << " / " << totalscore << "\n\n";
} else {
	cout << "\n\tWrong!\n"; // Wrong
	question += 1; // + 1 question amount
	cout << "Your current score: " << score << " / " << totalscore << "\n\n";
}


And I just noticed it would stop at question 2/3, rather than 3/3, so I added:
while (question < questionAmount + 1) {
And it seems to be working just fine now.

Thanks for the help, guys!
Consider putting this into a function called ShowMenu()

1
2
3
4
5
6
7
8
9
10
cout << "o------------------------------o\n";
cout << "|        2. Math Sheet         |\n";
cout << "o------------------------------o\n\n";

cout << "Select difficulty (or type 0 to go\n";
cout << "back to the menu):\n";
cout << "\t1. Easy (0-20)\n";
cout << "\t2. Medium (0-100)\n";
cout << "\t3. Hard (0-300)\n";
cout << "\t4. Extreme (0-1,000)\n\n";


Now for this part:
int score(0), totalscore(questionAmount);

Does that even compile? You can't separate statements with commas like that for a start.

and

int score(0)

should be

int score = 0;

Consider using a switch statement instead of this:
1
2
3
4
5
6
7
8
9
10
11
12
if (mathSheetDifficulty == 1) {
	difficulty = 20;
} else if (mathSheetDifficulty == 2) {
	difficulty = 100;
} else if (mathSheetDifficulty == 3) {
	difficulty = 300;
} else if (mathSheetDifficulty == 4) {
	difficulty = 1000;
} else if (mathSheetDifficulty == 0) {
	system("cls");
	goto themenu;
}


IF you don't know what a switch is then Google C++ switch example

also make your variable names shorter - mathSheetDifficulty is too long, why not just Difficulty?

question += 1; can be question++ ; (the increment operator.)

Hope this helps.



I'll look into ShowMenu(), never heard of that code before. Sounds interesting.

And yes, that does compile just fine. My C++ professor told me that's just not a "proper" way of coding. Doing score(0) works the same as = 0 (at least from what I know, it works just fine).

I actually just learned about switch statements last night. It just looks cleaner than all the if's but works the same (right?). I read this: http://www.cplusplus.com/doc/tutorial/control/ and it really helped. Might give it a re-read soon.

Names, yes. Need to shorten those, and if that saves two characters, sure why not. I forgot about the ++'s and --'s.

Thanks!
I'll look into ShowMenu(), never heard of that code before.


It's not a standard thing - just trying to introduce the idea of using functions is a good practice.
Topic archived. No new replies allowed.