Unreferenced local Variable

I have 4 warnings on this code
1- Unitialized local variable 'winnerAverage' used
2-4 lowest, isValid,highest are all unreferenced local variables

Also it says I have errors that all my score1-score5 are used without being initialized, they are at the top of the program though.

I am new to this and really teaching myself. How do I make this stupid thing work =(

Thanks SO much in advance

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



// Function Prototypes

	double ValidScore (double); // Ensures Scores are not lower than 1 or greater than 10. 
	double findLowest(double, double, double, double, double);
	double findHighest(double, double, double, double, double);
	double calcAvgScore(double, double, double, double, double);

	int main()
	{
	// variables for user input
	bool isValid;
	double score1, score2, score3, score4, score5, score;
	int highest, lowest; 
	double winnerAverage;
	string name, winnerName;
	
//Get Contestants names, when Done is entered loop quits
	
	do 
	{

	cout <<"Enter the name of the star, enter Done when there are no more stars.";
	cin >> name;
	cout << endl;
	
	 if (name == "Done" || name == "done")

	{
		cout <<"Enter Judge 1's Score: ";
		cin >> score;
		score1 = ValidScore (score);

		cout <<"Enter Judge 2's Score: ";
		cin >> score;
		score2 = ValidScore (score);

		cout <<"Enter Judge 3's Score: ";
		cin >> score;
		score3 = ValidScore (score);

		cout <<"Enter Judge 4's Score: ";
		cin >> score;
		score4 = ValidScore (score);

		cout <<"Enter Judge 5's Score: ";
		cin >> score;
		score5 = ValidScore (score);
		}
	 
	
	double average = calcAvgScore (score1, score2,score3,score4,score5);
    if (average > winnerAverage)
           {
            winnerAverage = average;
            winnerName = name;
    }

	} while (name == "Done" || name == "done");
	
	// Declaring Winner
	cout << setprecision(1) << fixed;
	cout << " ... and the winner is " <<winnerName <<" with a score of " <<winnerAverage <<endl;

	system ("pause");
		return 0;
	
	}

	// Calculating Averages (drops highest and lowest achieved score, then divides by remaining scores (3))

	double calcAvgScore(double score1, double score2, double score3, double score4, double score5)   // HERE IS WHERE A ; IS EXPECTED and LOCAL FUNCTION DEFINITIONS ARE ILLEGAL
	{
		double highest = findHighest(score1, score2, score3, score4, score5);
		double lowest = findLowest(score1, score2, score3, score4, score5);
		double total = (score1 + score2 + score3 + score4 + score5 - highest - lowest);
		double average = (total) / 3;
		return average;
	}
	
// Validate Score is not less than 1 or greater than 10

	double ValidScore (double score)
	{
			while ( score < 1 || score > 10)
			{
				cout <<" Please enter a score between 1-10: ";
				cin >> score;
			}
			return score;
	}
	
	// define function to find lowest score for each contestant
	double findLowest(double score1, double score2, double score3, double score4, double score5)
	{
		double lowest = score1;
		if (score2 < lowest)
			lowest = score2;
		if (score3 < lowest)
			lowest = score3;
		if (score4 < lowest)
			lowest = score4;
		if (score5 < lowest)
			lowest = score5;

		return lowest;
	}

	// define function to find highest score for each contestant
	double findHighest(double score1, double score2, double score3, double score4, double score5)
	{
		double highest = score1;
		if (score2 > highest)
			highest = score2;
		if (score3 > highest)
			highest = score3;
		if (score4 > highest)
			highest = score4;
		if (score5 > highest)
			highest = score5;

		return highest;
	}
  
Declaration is different from initialization. Declaring is making the variables, initialization is giving them values. Uninitialized variables will hold garbage values.

If the user does not enter "done" or "Done" your code still calculates average, even though the score variables will be uninitialized.

Also, see line 59: winnerAverage was never initialized. So saying if (average > winnerAverage) will give you undefined behavior (and warnings).

Your loop is also a bit stange. You want to keep looping as long as the user enters "Done"?

Topic archived. No new replies allowed.