-1

我正在做一项家庭作业,该作业需要我为每个学生生成 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 之外,一切都好了吗?

4

2 回答 2

2

问题之一如下:

while (!studentFile.eof())
{
    studentFile >> ssn >> grade;  //put read records here

    if (count == 0)
    {
        maxScore = grade;
        minScore = grade;
    }
    else
    {
        if (grade > maxScore){
            maxScore = grade;
            maxSSN = ssn
        }
        if (grade < minScore)
            minScore = grade;
    }
        totalGrades += grade ;
        count++;
  }//^^^^should end your while loop here, finish reading all then do average

    //^^^^^now compute the averageScore
    if (count)
    avgScore = totalGrades / (double) count;

首先尝试清除您的逻辑,如果有任何问题,您会发现其他问题。在 while 循环中,您不记得当前的最大 SSN。

于 2013-03-29T23:28:22.050 回答
0

您的输入循环仍然与您问题的先前版本不同。循环eof()是一种不好的做法,原因在另一个问题中进行了解释:为什么循环条件内的 iostream::eof 被认为是错误的?

尝试改变这个:

while (!studentFile.eof())
{
    studentFile >> ssn >> grade;
    // ... the rest of your code
}

到:

while (studentFile >> ssn >> grade)
{
    // ... the rest of your code
}
于 2013-03-30T00:39:33.810 回答