-1

Been working on GPA calculator, the problem I've been facing when running the program is even when the user inputs a grade "B" when asked to input grade the GPA still gives an output of 5, which is not supposed to be.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    string course;
    int courses;
    int a_ = 1;
    int units;
    int gp = 0;
    int tgp = 0;
    int totalunits = 0;
    string grade;
    float gpa;

    cout << "How many courses offered" << endl;
    cin >> courses;
    while (a_ <= courses){
        cout << "Type the course code" << endl;
        cin >> course;
        cout << "Units allotted to the course" << endl;
        cin >> units;
        cout << "Input Grade " << endl;
        cin >> grade;
        if (grade == "A" || "a"){
            gp = units * 5;
        }
        else if (grade == "B" || "b"){
            gp = units * 4;
        }
        else if (grade == "C" || "c") {
            gp = units * 3;
        }
        else if (grade == "D" || "d") {
            gp = units * 2;
        }
        else if (grade == "E" || "e") {
            gp = units * 1;
        }
        else if (grade == "F" || "f") {
            gp = units * 0;
        }
        else {
            cout << "Incorrect details, Re-Input them." << endl;
        }


        tgp = tgp + gp;
        totalunits = totalunits + units;
        ++a_;

    }
    gpa = tgp/totalunits;
    cout << tgp << endl;
    cout << totalunits << endl;
    cout << "Your GPA is : " << gpa << endl;
}

Changed the switch statement to an if statement due to an error I was getting.

rawdpiper
  • 27
  • 5

4 Answers4

3

If you force uppercase, it will simplify everything.

char grade;
cin >> grade;
grade = toupper(grade);
gp = units * ('F' - grade);
RAEC
  • 332
  • 1
  • 8
2

Try some conversion function like:

int points_from_grade(char grade) {
    if (isupper(grade)) {
        return 5 - (grade - 'A');
    } else { // islower
        return 5 - (grade - 'a');
    }
}
Mohit
  • 1,225
  • 11
  • 28
1

An interesting thing to note about characters in C++ is that they have an associated numerical value which can be used for mathematic operations.

Knowing this, one way you could accomplish what you set out to do is by taking the decimal ASCII value of the character 'A' (which is 65) and subtracting 60 from it which will give you the desired integer value for that letter grade.

For example:

cout << 'A' - 60;

Would output the integer '5'.

If the user enters a lowercase 'a' instead, you'll want to take the decimal ASCII value (which is 97) and subtract 92 from it.

Following that schema, you should be able to figure out what changes need to be made to get your program working the way you want it.

For reference, the full ASCII Table and Description can be found here: https://www.asciitable.com/

  • While this works, Mohit's example with `5 - (grade - 'A')` is far easier to discern intent from than `grade - 60` and by the time the compiler's through with it, the amount of work done by the program will be identical. – user4581301 Jul 03 '18 at 15:07
0

You could change your switch statement to something like this:

// Relevant code parts

const int GRADE_A_POINTS = 5;
const int GRADE_B_POINTS = 4;
// etc.

char grade;
cin >> grade;

switch (grade) {
    case 'A':
    case 'a':
        gp = units * GRADE_A_POINTS;
        break;

    case 'B':
    case 'b':
        gp = units * GRADE_B_POINTS;
        break;

    // etc.
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • Recommend adding a note about giving an identifier to 5 and 4 if only to prevent using [a dreaded Magic Number](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). – user4581301 Jul 03 '18 at 14:57
  • He could set the points per grade as a final variable somewhere in his code. I can add that to my post. – 0xCursor Jul 03 '18 at 14:58