0

I am trying to calculate the average for a class from scores given using a data file that was given.

The formula I'm using is grade_Average = sum / i;

The data file that was given is :

Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55

The output I am getting is

Johnson,Joe                    B
Caldwell,Susie                    D
Baker,Matt                    A
Anderson,Alex                    B
Dixon,Perry                    F

Class average inf

I am not sure if I have the formula wrong or if the formula is in the wrong place.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    // Variable declarations: 
    string fName[10];
    string lName[10];
    float grade_Average;
    string file;
    string name;
    int scores[10];
    float sum = 0;
    char grade;
    int i = 0;

    ifstream din;

    // Function body: 

    cout << "Enter the name of the file. " << endl;
    cin >> file;

    din.open(file.c_str());

    if (!din)
    {
        cout << " Cannot open the input file. Please try again." << endl;
        return 0;
    }

    cout << setw(10) << setfill(' ')  << "Name" <<setw(20)<<setfill(' ')<< "Grade" << endl;

    while (!din.eof())
    {

        din >> fName[i];
        din >> lName[i];
        din >> scores[i];

        sum = sum + scores[i];

        switch (static_cast<int> (scores[i]/10))
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            grade = 'F';
            break;
        case 6:
            grade = 'D';
            break;
        case 7:
            grade = 'C';
            break;
        case 8:
            grade = 'B';
            break;
        case 9:
            grade = 'A';
            break;
        case 10:
            grade = 'A';
            break;
        default:
            cout << "Invalid score." << endl;

            i++;
        }

        name = lName[i] + ',' + fName[i];
        cout << setw(10) << setfill(' ') << name  << setw(20) << setfill(' ')<<(" ") << grade << endl;


    } 
    grade_Average = sum / i;
    cout << "Class average " << grade_Average << endl;

    din.close();

    return 0;
}
bolov
  • 72,283
  • 15
  • 145
  • 224
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong, and use a debugger. – user657267 Nov 11 '18 at 06:16
  • Definitely use a debugger if you do c++ :) – L.C. Nov 11 '18 at 06:19
  • `i` is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statement`grade_average = sum/i` where `sum` and`grade_average` are of type `float`. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity. – Peter Nov 11 '18 at 09:03

2 Answers2

0

i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.

Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)

0

Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.

L.C.
  • 1,098
  • 10
  • 21