Repetition.

I am trying to create a program that will allow a user to input 5 letter grades and then it outputs the total grade points and the grade point average. I am having trouble converting the letter grade from the user into a value. If the user inputs "A" then it should equal 4, "B" should equal 3, etc. Any help is greatly appreciated. Thank you.

This is what I have so far. I have edited it from time of the original post now I am just not getting a correct GPA or total grade points. If I put "A" in five times I should get total grade point = 20 and GPA = 4.0 but instead I am getting 35 and GPA= 7.0.... please help me! THank You!

// CONSTANTS
const char gradeA='A';
const char gradeB='B';
const char gradeC='C';
const char gradeD='D';
const char gradeF='F';

// VARIABLES
char grade; // INPUT - Grade user inputs.
int gradePoints=0; // CALC & OUTPUT - Total grade points.
float gradeAvg; // CALC & OUTPUT - Grade point average.
int count; // PROCESS - Loop will run as count is less
// than or equal to 5.



//INPUT & PROCESSIONG - Program will convert letter grades to grade
// points, calculate grade points, and calculate
// grade point average.

cout << left;
cout << "TEST #1: " << endl; // Title and test number.

gradePoints=0;
count=1;

for(count=1; count<=5; count=count+1)
{
cout << right;
cout << setw(30) << "Enter letter grade #" << count << ":";
cin.get(grade);
cin.ignore(1000,'\n');
if(grade==gradeA)
{
gradePoints=gradePoints+4;
}
if(grade==gradeB)
{
gradePoints=gradePoints+3;
}
if(grade=gradeC)
{
gradePoints=gradePoints+2;
}
if(grade=gradeD)
{
gradePoints=gradePoints+1;
}
if(grade=gradeF)
{
gradePoints=gradePoints+0;
}
}
gradeAvg=gradePoints/5;

//OUTPUT: Total grade points and grade point average is displayed
// to the user.
cout << fixed << ;
cout << endl << setw(30) << "Total Grade Points: " << gradePoints;
cout << endl << setw(31) << "Grade Point Average: " << gradeAvg;

return 0;
}
Last edited on
You're assigning values to 'grade' in the if statements for grades C, D and F, which is affecting your overall grade.

You need to use the == operator for these statements. Additionally, I would if an if-else block here (or a switch statement), rather than a bunch of individual ifs.
Last edited on
Wow I totally forgot about the (==). I only used that for the first comparison. I was up until 2:00 am trying to figure it out. Much needed rest and your help definitely cleared things up. I switched the if-then statements to if-then-else. I have not covered and can not use a switch statement. Thanks again for the help. I really appreciate it. Here is my final code... any other suggestions?

// CONSTANTS - Assigning grades to variables in order to compare
// input from user to these constants and convert
// letter grade into grade points.
const char gradeA='A';
const char gradeB='B';
const char gradeC='C';
const char gradeD='D';
const char gradeF='F';

// VARIABLES
char grade; // INPUT - Grade user inputs.
int gradePoints=0; // CALC & OUTPUT - Total grade points.
float gradeAvg; // CALC & OUTPUT - Grade point average.
int count; // PROCESS - Loop will run as count is less
// than or equal to 5.


//INPUT & PROCESSIONG - Program will convert letter grades to grade
// points, calculate grade points, and calculate
// grade point average.

cout << left;
cout << "TEST #1: " << endl; // Title and test number.

gradePoints=0; // Initialize
count=1;

for(count=1; count<=5; count=count+1)
{
cout << right;
cout << setw(30) << "Enter letter grade #" << count << ":";
cin.get(grade);
cin.ignore(1000,'\n');

if(grade==gradeA)
{
gradePoints=gradePoints+4;
}
else
{
if(grade==gradeB)
{
gradePoints=gradePoints+3;
}
else
{
if(grade==gradeC)
{
gradePoints=gradePoints+2;
}
else
{
if(grade==gradeD)
{
gradePoints=gradePoints+1;
}
else
{
if(grade==gradeF)
{
gradePoints=gradePoints+0;
}
}
}
}
}
}

gradeAvg=gradePoints/(float)5;

//OUTPUT: Total grade points and grade point average is displayed
// to the user.
cout << setprecision(2) << fixed;
cout << endl << setw(30) << "Total Grade Points: " << gradePoints;
cout << endl << setw(31) << "Grade Point Average: " << gradeAvg;

return 0;
}
Alternatively could I just do this...
if(grade=='A')
{
gradePoints=gradePoints+4;
}
else
{
if(grade=='B')
{
gradePoints=gradePoints+3;
}
else
{
if(grade=='C')
{
gradePoints=gradePoints+2;
}
else
{
if(grade=='D')
{
gradePoints=gradePoints+1;
}
else
{
if(grade=='F')
{
gradePoints=gradePoints+0;
}
}
}
}
}
}


instead of creating constants for A,B,C,D,F can i just compare the inputed grade directly to the letter?
Use else-if rather than else statements with if statements inside them.
1
2
3
4
5
6
7
8
9
if(grade == 'A')
{
   // do stuff
}
else if(grade == 'B')
{
   // do stuff
}
// and so on 
Topic archived. No new replies allowed.