我正在写一个家庭作业,要求用户阅读一个包含未知数量的 SSN 和成绩的文本文件。然后程序需要计算最高、最低和平均分数,然后输出带有相关 SSN 的最高成绩以及最低和平均成绩。
它似乎运行正常,但输出全为“0”。
非常感谢任何解决此问题的帮助。
代码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string ssn, maxSSN;
int grade = 0, totalGrades = 0, count = 0, maxScore = 0, minScore = 0;
double avgScore = 0;
ifstream inFile;
inFile.open("C:\\lab5a.txt");
if(!inFile)
{
cout << "Error opening the requested file\n\n";
system("pause");
return 101;
}
while (!inFile.eof())
{
if (count == 0)
{
maxScore = grade;
minScore = grade;
}
else
{
if (grade > maxScore)
maxScore = grade;
if (grade < minScore)
minScore = grade;
}
grade += totalGrades;
count++;
}
if (count)
avgScore = totalGrades / count;
cout << "Maximum Score: " << maxScore << " Student SSN:" << maxSSN << "\n";
cout << "Minimum Score: " << minScore << "\n";
cout << "Average Score: " << avgScore << "\n";
// studentFile >> ssn >> grade;
inFile.close();
cout << "\n\n";
system("pause");
return 0;
}