Max Grade Problem

I'm having trouble figuring out how to calculate the highest grade (A,B,C,D) in my GPA program. I have everything else working.

using namespace std;

int main()
float i;
float totalScore = 0;
int ncourses, score, hour, qualitypoint;
float GPA;
int totalhour = 0;
char grade;

//Calculate GPA

cout << "Enter the number of courses you wish to calculate: ";
cin >> ncourses;

for(i = 0; i < ncourses; i++)
cout << "Enter hours of course " << (i+1) << ": ";
cin >> hour;
cout << "Please enter your grade: ";
cin >> grade;

if(grade == 'A' || grade == 'a') score = 4;
else if(grade == 'B' || grade == 'b') score = 3;
else if(grade == 'C' || grade == 'c') score = 2;
else if(grade == 'D' || grade == 'd') score = 1;
else if(grade == 'F' || grade == 'f') score = 0;
else score = 0;

qualitypoint = score * hour;

totalScore += qualitypoint;
totalhour += hour;


cout << endl;
cout << "Your attempted hours are: " << totalhour << endl << endl;

cout << "Your total quality points are: " << totalScore << endl << endl;

GPA =totalScore/totalhour;

cout << "Your GPA is: " << GPA << endl;

return 0;
First, please use the code tags (<> button to the right when you post or edit a post) as it makes code a lot easier to read. Thanks!

First, that last else (else score = 0;) is unnecessary, or rather you might want to output an error, since if not other statement above it matches the user obviously input something incorrectly. Course, you might want to include a continue statement with it so that the remaining portion of the for loop doesn't get exectured and the user can reinput the grade. Of course, that will also cause i to be incremented again too, so that might not work as you want. You could use a while loop I suppose too, but that's up to you.

Are you talking about the highest grade the user entered? If so you'll either have to store the grades in an array or similar container as the user input them, or calculate it on the fly. The problem here is that the user can input both upper and lower case letters to indicate the grade received, so a simple if (grade > highgrade) highgrade = grade; won't work.

What you might want to do is simply use score to indicate the highest grade, and use something similar to the if loop I just indicated to calculate that, then output the grade based off that. Something like:

if (score > highscore) highscore = score; // you could put this in the for the loop, right after the if/else series.

//then, later when you want to output the highest grade

char highgrade;
if (highscore == 4) highgrade = 'A';
else if (highscore == 3) highgrade = 'B';
else if (highscore == 2) highgrade = 'C';
else if (highscore == 1) highgrade = 'D';
else highgrade = 'F';

I'm sure there's a more elegant and concise way to do this, perhaps with an enumerator (although the enum might actually be more useful when calculating score instead of the if/else series you are using. I really wish they had reverse enums where you could have text based output automatically converted from integer input, basically the opposite of what enums do.)
Last edited on
Topic archived. No new replies allowed.