Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototype
void getScore(int& score, ifstream& inFile);
void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest);
int findLowest(int, int, int, int, int);
int grades(int, int, int, int, int);
int main()
{
ifstream inFile;
inFile.open("grades.txt");
int lowest;
int testScr1, testScr2, testScr3, testScr4, testScr5;
// I deleted all the semicolons after if()
// Semicolons after if() are a bad idea; they make the comparison meaningless because a semicolon is a complete statement that means do nothing.
// So the if() will do nothing and then run whatever was underneath.
getScore(testScr1, inFile);
lowest = testScr1;
getScore(testScr2, inFile);
if (lowest > testScr2)
lowest = testScr2;
getScore(testScr3, inFile);
if (lowest > testScr3)
lowest = testScr3;
getScore(testScr4, inFile);
if (lowest > testScr4)
lowest = testScr4;
getScore(testScr5, inFile);
if (lowest > testScr5)
lowest = testScr5;
calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5, lowest);
inFile.close();
return 0;
}
void getScore(int& score, ifstream& inFile)
{
inFile >> score;
}
void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest)
{
int sum;
double average;
sum = s1 + s2 + s3 + s4 + s5 - lowest;
average = sum / 4.0;
cout << setw(4) << fixed << showpoint << setprecision(2);
cout << "The avergae of the four highest scores are: " << average << endl;
}
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest = s1;
cout << "The lowest test score is: " << lowest << endl;
return lowest;
}
Comment