local variable problem

Hi i'm making a program that takes the average of 5 judges and drops the highest and lowest to give a final score. So I'm having a local variable problem with (highest lowest and the all the score's) I can't recall how to get them to hold throughout all the functions.

Any advice would be helpful thanks everyone!





#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes.
void getJudgeData(double &); // Store in reference variables.
void calcScore(double); // Calculate and return the average of the 3 scores.
double findHighest(); // Find and return the highest of the 5 scores
double findLowest(); // Find and return the lowest of the 5 scores.


// Variables
double score1, // Variable for the judges score.
score2,
score3,
score4,
score5,
average, // The average of the finalscore.
lowest,
totalscore, // All five scores added up.
finalscore, // The score total after the highest and lowest have been subtracted.
highest;

int main()
{
getJudgeData(score1);
getJudgeData(score2);
getJudgeData(score3);
getJudgeData(score4);
getJudgeData(score5);
void calcScore();
cout << "The average score of the 5 judges is" << average << "\n";
return 0;
}
// Function used to get judge scores.
void getJudgeData(double &score)
{
do
{
cout << "Enter Judge's score: ";
cin >> score;
if ((score < 0) || (score > 10))
cout << "The score must be between 0 and 10 please try again.";
} while ((score < 0) || (score > 10));
}
// Function used to get average.
void calcScore(double)
{
double findLowest;
double findHighest;
double total, lowest, highest, average;
totalscore = (score1 + score2 + score3 + score4 + score5);
finalscore = (totalscore - lowest - highest);
average = finalscore / 3;
}
// Function used to find the lowest number.
double findLowest()
{
double score1, score2, score3, score4, score5, lowest;
if ((score1 < score2) && (score1 < score3) && (score1 < score4) && (score1 < score5))
score1 = lowest;
else if ((score2 < score1) && (score2 < score3) && (score2 < score4) && (score2 < score5))
score2 = lowest;
else if ((score3 < score1) && (score3 < score2) && (score3 < score4) && (score3 < score5))
score3 = lowest;
else if ((score4 < score1) && (score4 < score2) && (score4 < score3) && (score4 < score5))
score4 = lowest;
else if ((score5 < score1) && (score5 < score2) && (score5 < score3) && (score5 < score4))
score5 = lowest;
return lowest;
}
//Function used to find the highest number.
double findHighest()
{
double score1, score2, score3, score4, score5, highest;
if ((score1 > score2) && (score1 > score3) && (score1 > score4) && (score1 > score5))
score1 = highest;
else if ((score2 > score1) && (score2 > score3) && (score2 > score4) && (score2 > score5))
score2 = highest;
else if ((score3 > score1) && (score3 > score2) && (score3 > score4) && (score3 > score5))
score3 = highest;
else if ((score4 > score1) && (score4 > score2) && (score4 > score3) && (score4 > score5))
score4 = highest;
else if ((score5 > score1) && (score5 > score2) && (score5 > score3) && (score5 > score4))
score5 = highest;
return highest;
}
you're doing your assignments backwards in your find functions. Assignment stores the variable to the right of = to the variable left of the =. So 'lowest' and 'highest' are never being assigned a value.

also, the scores to be tested are never passed to those functions, you're basically just testing a bunch of random data in memory... now that I look at it more closely, there's all kinds of problem here. I'll be back with a more detailed analysis.
Last edited on
Thanks for taking the time to go over it man i really appreciate 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes.
void getJudgeData( double & ); // Store in reference variables.
void calcScore( double ); // Calculate and return the average of the 3 scores.
double findHighest(); // Find and return the highest of the 5 scores
double findLowest(); // Find and return the lowest of the 5 scores.


// Variables
double score1, // Variable for the judges score.
score2,
score3,
score4,
score5,
average, // The average of the finalscore.
lowest,
totalscore, // All five scores added up.
finalscore, // The score total after the highest and lowest have been subtracted.
highest;

int main()
{
	getJudgeData( score1 );
	getJudgeData( score2 );
	getJudgeData( score3 );
	getJudgeData( score4 );
	getJudgeData( score5 );
	void calcScore();
	cout << "The average score of the 5 judges is" << average << "\n";
	return 0;
}


// Function used to get judge scores.
void getJudgeData( double &score )
{
	do
	{
		cout << "Enter Judge's score: ";
		cin >> score;
		if( (score < 0) || (score > 10) )
			cout << "The score must be between 0 and 10 please try again.";
	} while( (score < 0) || (score > 10) );
}


// Function used to get average.
void calcScore( double )
{
	totalscore = (score1 + score2 + score3 + score4 + score5);
	finalscore = (totalscore - lowest - highest);
	average = finalscore / 3;
}


// Function used to find the lowest number.
double findLowest()
{
	if( (score1 < score2) && (score1 < score3) && (score1 < score4) && (score1 < score5) )
		lowest = score1;
	else if( (score2 < score1) && (score2 < score3) && (score2 < score4) && (score2 < score5) )
		lowest = score2;
	else if( (score3 < score1) && (score3 < score2) && (score3 < score4) && (score3 < score5) )
		lowest = score3;
	else if( (score4 < score1) && (score4 < score2) && (score4 < score3) && (score4 < score5) )
		lowest = score4;
	else if( (score5 < score1) && (score5 < score2) && (score5 < score3) && (score5 < score4) )
		lowest = score5;
	return lowest;
}


//Function used to find the highest number.
double findHighest()
{
	if( (score1 > score2) && (score1 > score3) && (score1 > score4) && (score1 > score5) )
		highest = score1;
	else if( (score2 > score1) && (score2 > score3) && (score2 > score4) && (score2 > score5) )
		highest = score2;
	else if( (score3 > score1) && (score3 > score2) && (score3 > score4) && (score3 > score5) )
		highest = score3;
	else if( (score4 > score1) && (score4 > score2) && (score4 > score3) && (score4 > score5) )
		highest = score4;
	else if( (score5 > score1) && (score5 > score2) && (score5 > score3) && (score5 > score4) )
		highest = score5;
	return highest;
}


compare your code to my modifications above. First, I reversed the assignments in the find functions.

The other big problem was that in both the find functions and in calcScore, you were declaring local versions of your global variables. For example, in findHighest() the line double score1, score2, score3, score4, score5, highest; declared new local variables with the same names as the global data. This effectively hides the global data and makes the function operate on the local versions, which are uninitialized and thus just hold whatever junk happened to be in the memory allocated for them.
Awesome dude, it's works, thanks! Only last problem is my answer always comes out to 0, do you have a solution for that?
line 31. should be no 'void' there. Also, the definition of calcScore() says it takes a double argument, but the function does not use an argument at all. Since the definition specifies an argument, and the use on line 31 does not pass one, this will cause an error.
I adjusted it but still receiving 0 as well as two errors. Error: function does not take 0 arguments. and the other one Error: too few arguments in function call. Both errors are referring to my calcScore() function.
make sure you update both the function and it's prototype to have no arguments.
Alright so this is my most recent program and i am receiving "unresolved externals" and "LNK2019: unresolved external symbol "void__cdecl calcScore(void)" (?calcScore@@YAXXZ) referenced in function"



#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes.
void getJudgeData(double &); // Store in reference variables.
void calcScore(); // Calculate and return the average of the 3 scores.
double findHighest(); // Find and return the highest of the 5 scores
double findLowest(); // Find and return the lowest of the 5 scores.


// Variables
double score1, // Variable for the judges score.
score2,
score3,
score4,
score5,
average, // The average of the finalscore.
lowest,
totalscore, // All five scores added up.
finalscore, // The score total after the highest and lowest have been subtracted.
highest;

int main()
{
getJudgeData(score1);
getJudgeData(score2);
getJudgeData(score3);
getJudgeData(score4);
getJudgeData(score5);
calcScore();
cout << "The average score of the 5 judges is " << average << "\n\n";
return 0;
}
// Function used to get judge scores.
void getJudgeData(double &score)
{
do
{
cout << "Enter Judge's score: ";
cin >> score;
if ((score < 0) || (score > 10))
cout << "The score must be between 0 and 10 please try again.";
} while ((score < 0) || (score > 10));
}
// Function used to get average.
void calcScore()
{
double findLowest;
double findHighest;
totalscore = (score1 + score2 + score3 + score4 + score5);
finalscore = (totalscore - lowest - highest);
average = finalscore / 3;
}
// Function used to find the lowest number.
double findLowest()
{
if ((score1 < score2) && (score1 < score3) && (score1 < score4) && (score1 < score5))
lowest = score1;
else if ((score2 < score1) && (score2 < score3) && (score2 < score4) && (score2 < score5))
lowest = score2;
else if ((score3 < score1) && (score3 < score2) && (score3 < score4) && (score3 < score5))
lowest = score3;
else if ((score4 < score1) && (score4 < score2) && (score4 < score3) && (score4 < score5))
lowest = score4;
else if ((score5 < score1) && (score5 < score2) && (score5 < score3) && (score5 < score4))
lowest = score5;
return lowest;
}
//Function used to find the highest number.
double findHighest()
{
if ((score1 > score2) && (score1 > score3) && (score1 > score4) && (score1 > score5))
highest = score1;
else if ((score2 > score1) && (score2 > score3) && (score2 > score4) && (score2 > score5))
highest = score2;
else if ((score3 > score1) && (score3 > score2) && (score3 > score4) && (score3 > score5))
highest = score3;
else if ((score4 > score1) && (score4 > score2) && (score4 > score3) && (score4 > score5))
highest = score4;
else if ((score5 > score1) && (score5 > score2) && (score5 > score3) && (score5 > score4))
highest = score5;
return highest;
}
look at the first two lines of calcScore(). you are not calling the findLowest() and findHighest() functions. You are declaring new variables with those names.
One more thing is preventing it from being perfect. highest and lowest aren't being subtracted from the totalscore before the average is taken, do you have a suggestion on that?
post your code as it is now so I can look it over.
#include <iostream>
#include <cmath>
using namespace std;

//Function Prototypes.
void getJudgeData(double &); // Store in reference variables.
void calcScore(); // Calculate and return the average of the 3 scores.
double findHighest(); // Find and return the highest of the 5 scores
double findLowest(); // Find and return the lowest of the 5 scores.


// Variables
double score1, // Variable for the judges score.
score2,
score3,
score4,
score5,
average, // The average of the finalscore.
lowest,
totalscore, // All five scores added up.
finalscore, // The score total after the highest and lowest have been subtracted.
highest;

int main()
{
getJudgeData(score1);
getJudgeData(score2);
getJudgeData(score3);
getJudgeData(score4);
getJudgeData(score5);
calcScore();
cout << "The average score of the 5 judges is " << average << "\n\n";
system("pause");
return 0;
}
// Function used to get judge scores.
void getJudgeData(double &score)
{
do
{
cout << "Enter Judge's score: ";
cin >> score;
if ((score < 0) || (score > 10))
cout << "The score must be between 0 and 10 please try again.";
} while ((score < 0) || (score > 10));
}
// Function used to find the lowest number.
double findLowest()
{
if ((score1 < score2) && (score1 < score3) && (score1 < score4) && (score1 < score5))
lowest = score1;
else if ((score2 < score1) && (score2 < score3) && (score2 < score4) && (score2 < score5))
lowest = score2;
else if ((score3 < score1) && (score3 < score2) && (score3 < score4) && (score3 < score5))
lowest = score3;
else if ((score4 < score1) && (score4 < score2) && (score4 < score3) && (score4 < score5))
lowest = score4;
else if ((score5 < score1) && (score5 < score2) && (score5 < score3) && (score5 < score4))
lowest = score5;
return lowest;
}
//Function used to find the highest number.
double findHighest()
{
if ((score1 > score2) && (score1 > score3) && (score1 > score4) && (score1 > score5))
highest = score1;
else if ((score2 > score1) && (score2 > score3) && (score2 > score4) && (score2 > score5))
highest = score2;
else if ((score3 > score1) && (score3 > score2) && (score3 > score4) && (score3 > score5))
highest = score3;
else if ((score4 > score1) && (score4 > score2) && (score4 > score3) && (score4 > score5))
highest = score4;
else if ((score5 > score1) && (score5 > score2) && (score5 > score3) && (score5 > score4))
highest = score5;
return highest;
}
// Function used to get average.
void calcScore()
{
totalscore = (score1 + score2 + score3 + score4 + score5);
finalscore = (totalscore - lowest - highest);
average = finalscore / 3;
}
the problem is that findLowest() and findHighest() never actually get called. The modifications I was talking about last time to calcScore() should look like this:
1
2
3
4
5
6
7
8
void calcScore()
{
	findLowest();
	findHighest();
	totalscore = (score1 + score2 + score3 + score4 + score5);
	finalscore = (totalscore - lowest - highest);
	average = finalscore / 3;
}
Last edited on
Topic archived. No new replies allowed.