Variable "--" being used without being initialized

Im trying to have the output display message saying "Your score was not a perfect score" If the average is below 100 (entering numbers such as 1,2,3). When I try to test it out I get a error saying "The variable 'perfectScore' (line32) is being used without being initialized" . But if i test it out with the average being above 100 I get no error and after loop if I attempt the average below 100 no error. This error happened to me in the past I've tried to look at the braces but still can't figure out y this happens.
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
  #include <iostream> //for cout and cin
#include <conio.h>  // for getch

using namespace std;

int main()
{			
		//Variables
		int score1, score2, score3;	   // three test scores from user
		double average;				  //average and perfect score
		char response;				   //response from user
		bool perfectScore;
	do
	{
		//Get test scores from User
		cout << "Enter your 3 test scores and I will average them:" << endl;
		cin >> score1 >> score2 >> score3;

		//Calculate average score
		average = (score1 + score2 + score3) / 3.0;

		//Display message
		if (average >= 100)
		{
			perfectScore = false; //Set the flag variable
			cout << "Congratulations!\n";
			cout << "That's a perfect score.\n";
			cout << "You deserve a pat on the back.\n";
		}
		else
		{
			if (!(perfectScore))
		
			cout << "Your average is " << average << endl;
			cout << "Your score was not a perfect score." << endl;
		}
			//Ask user whether to continue or not, and get a response
        cout << "Do you want to continue?\n"
             << "Press y for Yes, any letter for No, and then press Enter: ";
        cin >> response;


	} while (response == 'y' || response == 'Y');
	_getch(); //Hold the Screen

	return 0;

}
Last edited on
There are several points here. First, when a variable is not initialised, it will take some random value dependent upon the contents of the memory location where the variable is stored. This value is unpredictable. So if the program runs without error, you may have just been lucky that the variable was set to a "sensible" default value.

Another point is that the compiler will identify a possible path through the code where the uninitialised value may be used, but the compiler can't tell how likely it is that the particular path will be taken.

In this case, if the condition at line 23 is false, that is (average < 100), the else block will be executed, and there the value of perfectScore is tested - but it has not yet been initialised.

One more point here, there is an outer loop, controlled by the response == 'Y', and the variable needs to be re-initialised at the start of each pass through this outer loop, otherwise, the variable will retain its value from the previous set of data. This is something which the compiler would not necessarily warn about - it can't read your mind and tell what you were trying to do.

And finally ... Lines 34 and 35 are indented to the same level,which is deceptive. line 34 is controlled by the preceding 'if', but line 35 is not.
Last edited on
Topic archived. No new replies allowed.