0

I am writing a homework assignment that asks for the user to read in a text file that contains an unknown number of SSNs and grades. The program then needs to compute the max, min and average scores, then output the max grade with the associated SSN as well as the min and avg grades.

It seems to operate correctly but the output is all "0".

Any help fixing this is greatly appreciated.

Code:

#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;

}
Kevin Schultz
  • 886
  • 3
  • 20
  • 42

2 Answers2

3

1.You are not reading anything from inFile. Most likely, you want to read the grade value:

 while (!inFile.eof())
 {
     inFile >> grade;
     ...

According to the feedback from @Blastfurnace, using inFile.eof() is also wrong. Consider the following input file:

3
5
9
17

where each line is terminated with lf. Then, the loop will read 5 lines since it does not detect the end of file after the value 17 has been read, but will try to read another value from the file. count will then be 5 when the loop terminates, which is wrong.

Use the following condition instead:

 while (inFile >> grade)
 {
   ...

To also read the ssn, use this:

while (inFile >> ssn >> grade)
{
   ...

This ensures that both ssn and grade have been read from the file when executing the loop body.

2.The calculation of the totalGrades has the operands in the wrong order:

 grade += totalGrades;

needs to be

 totalGrades += grade;

3.You have declared avgScore as double, but

 avgScore = totalGrades / count;

does integer arithmetic, so that you will never get a floating point result. You need to cast at least one operand to double:

 avgScore = totalGrades / (double) count;

4.What is still missing is the handling of the SSN

You need to read it along with the grade from the file, and then assign it to maxSSN at the same place where you assign the maxScore value.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
0

You are not reading the file and hence not taking grade as an input.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71