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
|
#include <iostream>
#include<iomanip>
using namespace std;
void getScore(int&, int&, float&, float&);
void calcAverage (double&,int, int&, float);
int findLowest(int&);
int main ()
{
float Total, score;
int numTest, leastscore=100;
double average;
cout << fixed << showpoint << setprecision(2);
getScore(numTest, leastscore, Total, score);
findLowest (leastscore); // (1) you don't use the returned value. (2) this merely prints a value
calcAverage(average,numTest,leastscore,Total);
return 0;
}
void getScore(int &numTest, int &leastscore, float &Total, float &score)
{
// Gets the number of test scores
cout << "How many test scores are you entering? ";
cin >> numTest;
Total=0;
int test =0;
while (test <=numTest) // you will read numTest+1 values
{
cout << "Enter test " << test<< " score now: ";
cin >> score;
if ( score < 1 || score > 100)
{
cout << "\nYou have entered an invalid number. \n\n"
<<"Re-Enter: ";
cin >> score; // you don't test this value
}
else if(score>0 && score <=100)
{
if ( score < leastscore)
leastscore=score;
test++;
}
Total+=score;
// you could have gotten bad value on line 40, but you would
// add it anyway, without updating the 'test'
}
}
void calcAverage (double& average,int numTest, int &leastscore, float Total)
{
// function declaration inside functions are legal, but uncommon
// you have declared these on lines 6 and 7
// you don't call getScore or calcAverage in this function
// void getScore(int numTest, int leastscore, float Total, float score);
// void calcAverage (double average);
average = (Total - leastscore) / ( (numTest) - 1); // What if numTest<2 ?
cout << "\nThe average score is: "<<average<<". \n";
}
int findLowest(int& leastscore)
{
// function declaration inside functions are legal, but uncommon
// you have declared getScore on line 6
// you don't call getScore in this function
// void getScore(int numTest,int leastscore, float Total, float score);
cout <<"\nThe lowest score is: "<<leastscore;
return leastscore; // leastscore was not modified here
}
|