-1

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");
}

Output after code change.

All better except I am not getting the maxScore SSN to show up?

Kevin Schultz
  • 886
  • 3
  • 20
  • 42
  • 1
    Try debugging the program and see where it fails to behave like expected. –  Mar 29 '13 at 23:25

2 Answers2

2

One of the issue is the following:

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;

Try to clear your logic at first, you will spot other issues if there is any. Inside the while loop, you did not remember the current maximum SSN.

taocp
  • 23,276
  • 10
  • 49
  • 62
0

You input loop is still broken from the previous version of your question. Looping on eof() is a bad practice and the reason is explained in this other question: Why is iostream::eof inside a loop condition considered wrong?

Try changing this:

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

to:

while (studentFile >> ssn >> grade)
{
    // ... the rest of your code
}
Community
  • 1
  • 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • I know that it's not great, but that statement is provided by the instructor for some reason. – Kevin Schultz Mar 30 '13 at 00:53
  • @KevinSchultz: Unfortunately, the code your instructor provided can cause a phantom "double read" of the last input line. See this other related question: [Testing stream.good() or !stream.eof() reads last line twice](http://stackoverflow.com/q/4324441/445976) – Blastfurnace Mar 30 '13 at 00:57