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;
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;
}
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;
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
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
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";