lundi 19 juin 2017

How do I stop these if else statements from compounding?

I'm trying to write a c++ program that reads input from a text file and assigns grades using a ten point grading scale then prints the results onscreen. I think my issue may be with the if else statements in the function deriveGrade, rather than incrementing the enum, they seem to be suming up the increments. Any help would be appreciated, thanks.

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

using namespace std;
int deriveGrade(double avarage);
enum letter_grade { A, B, C, D, F };

namespace tenPoint
{
    letter_grade deriveGrade(double avarage);
    char grade;
}

using namespace tenPoint;

int main()
{
    string name;
    double average;

    ifstream inData;    // Is the variable for input data from the file.

    inData.open("student_status.txt", ios::in);

    while (!inData.eof())
    {

        getline(inData, name);
        inData >> average;
        inData.ignore();
        grade = ::deriveGrade(average);

        cout << name << " " << average << " " << char(grade) << endl;

    }

    inData.close();

    return 0;
}


int deriveGrade(double average)
{


    if (average >= 90)
    {
        grade = static_cast<letter_grade>(grade + 65);
    }
    else if (average >= 80 && average < 90)
    {
        grade = static_cast<letter_grade>(grade + 1);
    }
    else if (average >= 70 && average < 80)
    {
        grade = static_cast<letter_grade>(grade + 2);
    }
    else if (average >= 60 && average < 70)
    {
        grade = static_cast<letter_grade>(grade + 3);
    }
    else if (average <= 50)
    {
        grade = static_cast<letter_grade>(grade + 4);
    }
    else
    {
        cout << "Invalid entry." << endl;
    }

    return grade;
}

Input from file:

Doe, John K.

93.2

Andrews, Susan S.

84.7

Monroe, Marylin

75.1

Gaston, Arthur C.

62.8

Harpo, Joanie Y.

42.7

Ginger, Fred T.

95.8

Program output:

Doe, John K. 93.2 A

Andrews, Susan S. 84.7 B

Monroe, Marylin 75.1 D

Gaston, Arthur C. 62.8 G

Harpo, Joanie Y. 42.7 K

Ginger, Fred T. 95.8 î

Press any key to continue . . .

Aucun commentaire:

Enregistrer un commentaire