I am working on a homework assignment that requires I generate the SSN and grade for each student as well as calculate the min, max and avg score while displaying the SSN of the student with the max score. It all works except I get an iteration of the min, max and avg for every student instead of the total and I dont get the SSN for the max score student. The data is read in from a file.
I need the output to look something like:
123-45-6789 87
123-56-9872 91
etc....
Then end with
Maximum Score: 91 SSN: 123-45-6789
Minimum Score: 23
Average Score: 86
Here is the code, any help would be awesome.
#include <iostream>
#include <string>
#include <fstream> // for file I/O
#include <iomanip>
using namespace std;
int main ()
{
string ssn, maxSSN;
int grade = 0, totalGrades = 0, count = 0, maxScore = 0, minScore = 0;
double avgScore = 0;
ofstream printFile ("StudentGrades.txt");
if (!printFile)
{
cout << " Error opening printFile" << endl;
system ("pause");
return 100;
}
//ifstream studentFile ("lab5a.dat"); // alternative open
ifstream studentFile;
studentFile.open("c:\\lab5a.dat");
if (!studentFile)
{
cout << "Open error on lab5a.dat" << endl;
system ("pause");
return 101;
}
cout << " SSN Grade" << endl;
cout << "--------- -----" << endl;
while (!studentFile.eof())
{
studentFile >> ssn >> grade;
if (count == 0)
{
maxScore = grade;
minScore = grade;
}
else
{
if (grade > maxScore)
maxScore = grade;
if (grade < minScore)
minScore = grade;
}
totalGrades += grade ;
count++;
cout << ssn << setw(7) << grade << endl;
}
if (count)
avgScore = totalGrades / (double) count;
cout << "\n\n";
cout << "Maximum Score: " << maxScore << " Student SSN:" << maxSSN << endl;
cout << "Minimum Score: " << minScore << "\n";
cout << "Average Score: " << avgScore << "\n";
//studentFile >> ssn >> grade;
printFile << ssn;
if (grade >= 90)
printFile << " A" << endl;
else if (grade >= 80)
printFile << " B" << endl;
else if (grade >= 70)
printFile << " C" << endl;
else if (grade >= 60)
printFile << " D" << endl;
else
printFile << " F " << endl;
studentFile.close();
printFile.close();
system ("pause");
}
All better except I am not getting the maxScore SSN to show up?