Dropping lowest score and finding average

So I'm supposed to collect five test scores, drop the lowest one, and find the average of the remaining four. It's required that we call getScores five times to retrieve the scores. The average always comes out to 0 and I can't figure out why.
-Thanks!

#include<iostream>
using namespace std;

void getScore()
{
int score;
cout << "Please enter a test score:\n";
cin >> score;
if(score <= 0 || score >= 100)
{
cout << "Invalid! Score must be between 1 and 100. Please enter a valid score.\n";
cin >> score;
}

}

int findLowest(int test1, int test2, int test3, int test4, int test5, int lowest)
{
lowest = test1;

if(lowest > test2)
{
lowest = test2;
}
if(lowest > test3)
{
lowest = test3;
}
if(lowest > test4)
{
lowest = test4;
}
if(lowest > test5)
{
lowest = test5;
}

return lowest;
}

void calcAverage(int test1, int test2, int test3, int test4, int test5)
{
int lowest = findLowest(test1, test2, test3, test4, test5, lowest);
int average = ((test1 + test2 + test2 + test4 + test5) - lowest)/4;
cout << "The average of the four highest test scores is: " << average << "\n";
}

int main()
{
int average, score, lowest;

getScore();
int &test1 = score;
getScore();
int &test2 = score;
getScore();
int &test3 = score;
getScore();
int &test4 = score;
getScore();
int &test5 = score;

calcAverage(test1, test2, test3, test4, test5);
//I can't figure out why the average always comes out to zero here
return 0;
}
getScore doesn't return anything, so all the test scores are garbage.

Functions should do conceptually one thing, there is too much going on in the calcAverage function.

See my other post in answer to your other question :+)
So I adjusted the program to this:

#include<iostream>
using namespace std;

double getScore()
{
double score;
cout << "Please enter a test score:\n";
cin >> score;
if(score <= 0 || score >= 100)
{
cout << "Invalid! Score must be between 1 and 100. Please enter a valid score.\n";
cin >> score;
}
return score;

}

double findLowest(double test1, double test2, double test3, double test4, double test5)
{
double lowest = test1;

if(lowest > test2)
{
lowest = test2;
}
if(lowest > test3)
{
lowest = test3;
}
if(lowest > test4)
{
lowest = test4;
}
if(lowest > test5)
{
lowest = test5;
}

return lowest;
}

double calcAverage(double test1, double test2, double test3, double test4, double test5)
{
double average = 0.0;
double lowest = 0.0;

lowest = findLowest(test1, test2, test3, test4, test5);
return average = ((test1 + test2 + test2 + test4 + test5) - lowest)/4;

}

int main()
{
double average, score, lowest;

getScore();
double &test1 = score;
getScore();
double &test2 = score;
getScore();
double &test3 = score;
getScore();
double &test4 = score;
getScore();
double &test5 = score;

calcAverage(test1, test2, test3, test4, test5);
cout << "The average of the four highest test scores is: " << average << "\n";

return 0;
}

And this is how it runs:

Please enter a test score:
33
Please enter a test score:
44
Please enter a test score:
55
Please enter a test score:
66
Please enter a test score:
77
The average of the four highest test scores is: 720558736

Any suggestions?
You never assign any value to average.

Also, you never do anything with the value returned from calcAverage.

(Hint: these two things are not entirely unrelated.)
Any suggestions?

yes, viz. consider using std::vector<double> to store the scores and your program would be much shorter applying std::vector methods and other standard library functions, algorithms for average, max, min etc scores
Last edited on
Let's look at this:
return average = ((test1 + test2 + test2 + test4 + test5) - lowest)/4;

You are adding test2 twice. Also return average = should be more like,
return ((test1 + test2 + test3 + test4 + test5) - lowest) / 4;
OR
1
2
    average = ((test1 + test2 + test3 + test4 + test5) - lowest) / 4;
    return average;


Let's look at this portion.
1
2
3
4
5
6
7
8
9
10
getScore();
double &test1 = score;
getScore();
double &test2 = score;
getScore();
double &test3 = score;
getScore();
double &test4 = score;
getScore();
double &test5 = score;


You call the function getScore(), but you are not returning the value of each score.
Perhaps, something like this...
1
2
3
4
5
    double test1 = getScore();
    double test2 = getScore();
    double test3 = getScore();
    double test4 = getScore();
    double test5 = getScore();


Let's look at this portion...
calcAverage(test1, test2, test3, test4, test5);
Same issue, you return the value, but there are no variables to obtain the average.
So, something like this could help...
1
2
    average = calcAverage(test1, test2, test3, test4, test5);
    cout << "The average of the four highest test scores is: " << average << "\n";


OR

cout << "The average of the four highest test scores is: " << calcAverage(test1, test2, test3, test4, test5) << "\n";
Topic archived. No new replies allowed.