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.