我正在做一项家庭作业,该作业需要我为每个学生生成 SSN 和成绩,并计算最低、最高和平均分数,同时显示具有最高分数的学生的 SSN。除了我得到每个学生的最小值、最大值和平均值的迭代而不是总数之外,这一切都有效,而且我没有得到最高分数学生的 SSN。数据是从文件中读入的。
我需要输出看起来像:
123-45-6789 87
123-56-9872 91
等....
然后以最高分结束:91 SSN:123-45-6789
最低分:23
平均分:86
这是代码,任何帮助都会很棒。
#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");
}
除了我没有显示 maxScore SSN 之外,一切都好了吗?