I can't believe how out of hand this has gotten.
I think we need to go back to the last program that kind-of sort-of worked.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototype
void getScore(int& score, ifstream& inFile);
void calcAverage(int, int, int, int, int);
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);
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 sum;
int lowest;
double average;
lowest = findLowest(s1, s2, s3, s4, s5);
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;
}
Now I remember that the problem was that the first number in grades.txt was assumed to be the lowest. But look at the code in main(). You read in a number, assume it is the lowest. Then you read in another number and compare it with the current lowest to see if there is a new, lower number. You keep doing that. That is right. After all the numbers are entered, lowest is correct.
You don't need to call findLowest() any more. Delete everything to do with that function.
What you ought to do next is change calcAverage() to receive the lowest number from main(), which is where you calculated it. You do this by passing in an argument. So here you go. I will give you the prototype. You will have to change the definition and the call of calcAverage() yourself.
Code:
void calcAverage(int s1, int s2, int s3, int s4, int s5, int lowest);
This is all I wanted you to do the last time I posted. I'm sorry if I was unclear.