Ok so my assignment is as follows:
Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions:
void getScore() should ask the user for a test score, store it in a refrence parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main, and should be passed the five scores.
int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage, which uses the function to determine which of the five scores to drop.
Input Validation: Do not accept test scores lower than 0 or higher than 100.
So below is my program, but when I run it the following message occurs after the lowest score is found:
DeBug Error!
Run-Time check failure #3- The variable "average" is being used without being initialized.
I understand that this means I have an issue with my average line, but I'm unsure as to how to resolve this issue.
Any guidance would be GREAT.
//Lowest Score Drop
#include <iostream>
using namespace std;
void intro();
void getScore(float&);
void calcAverage(float, float, float, float, float);
int findLowest(float, float, float, float, float);
int main()
{
float score1,
score2,
score3,
score4,
score5;
intro();
getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);
calcAverage(score1, score2, score3, score4, score5);
system("pause");
return 0;
}
void intro()
{
cout << "This program calculates the average of a group of test scores\n";
cout << "where the lowest score in the group is dropped.\n\n";
}
void getScore(float &score)
{
score;
cout << "Please enter a test score. ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Invalid entry! Cannot accept test scores lower than 0 or higher than 100.\n";
cout << "Please re-enter a test score. ";
cin >> score;
}
}
void calcAverage(float grd1, float grd2, float grd3, float grd4, float grd5)
{
float lowest, average;
lowest = findLowest(grd1, grd2, grd3, grd4, grd5);
cout << lowest << endl << endl;
if (lowest == 1)
{
average = (grd2 + grd3 + grd4+ grd5)/4;
cout << "Four highest test scores: " << fixed << grd2 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
}
else if (lowest == 2)
{
average = (grd1 + grd3 + grd4+ grd5)/4;
cout << "Four highest test scores: " << fixed << grd1 << ", " << grd3 << ", " << grd4 << ", " << grd5 << endl;
}
else if (lowest == 3)
{
average = (grd1 + grd2 + grd4+ grd5)/4;
cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd4 << ", " << grd5 << endl;
}
else if (lowest == 4)
{
average = (grd1 + grd2 + grd3+ grd5)/4;
cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd5 << endl;
}
else if (lowest == 5)
{
average = (grd1 + grd2 + grd3+ grd4)/4;
cout << "Four highest test scores: " << fixed << grd1 << ", " << grd2 << ", " << grd3 << ", " << grd4 << endl;
}
cout << "Average: " << fixed << average << endl;
}
int findLowest(float score1, float score2, float score3, float score4, float score5)
{
float lowest = score1;
int index = 1;
cout<<"The lowest of the five scores is:\n"<<endl;
if (lowest > score2)
{
lowest = score2;
index = score2;
}
if (lowest > score3)
{
lowest = score3;
index = score3;
}
if (lowest > score4)
{
lowest = score4;
index = score4;
}
if (lowest > score5)
{
lowest = score5;
index = score5;
}
return index;
}