I feel like I am getting very close! I just need a little more help :)

Okay, so I need to write a program that will take in 5 letter grades, and will convert them to grade points. Then I need to output the sum of the grade points, and the average GPA of the 5 grades. My teacher wants us to use nested if-then-else statements. For the code, I have this so far:
int main()
{
char grade1; //The first grade that is inputed - INPUT

char grade2; //The second grade that is inputed - INPUT

char grade3; //The third grade that is inputed - INPUT

char grade4; //The fourth grade that is inputed - INPUT

char grade5; //The fifth grade that is inputed - INPUT

float grade1num; //The converted value from a letter grade A to
//grade point value(4.0) - CALC

float grade2num; //The converted value from a letter grade B to
//grade point value(3.0) - CALC

float grade3num; //The converted value from a letter grade C to
//grade point value(2.0) - CALC

float grade4num; //The converted value from a letter grade D to
//grade point value(1.0) - CALC

float grade5num; //The converted value from a letter grade F to
//grade point value(0) - CALC

float gradesum; //The total value of the 5 grade points combined -
//CALC & OUTPUT

float gradeTotal; //The average of the 5 grade point values - CALC &
//OUTPUT 

//Variables - These are the 5 prompts that are given to the user for
// the grade input, and then they are stored into their
// values
cout << "Enter Letter Grade #1: " << endl;
cin >> grade1;

cout << "Enter Letter Grade #2: " << endl;
cin >> grade2;

cout << "Enter Letter Grade #3: " << endl;
cin >> grade3;

cout << "Enter Letter Grade #4: " << endl;
cin >> grade4;

cout << "Enter Letter Grade #5: " << endl;
cin >> grade5;


//Process- The grades are converted from their letter grade into
// their numeric value. The grades are then totaled, and are
// averaged.

if (grade1 == 'A')
{
grade1num = 4.0;
}

else
{

if (grade2 == 'B')
{
grade2num = 3.0;
}
else
{

if (grade3 == 'C')
{
grade3num = 2.0;
}
else
{
if (grade4 == 'D')
{
grade4num = 2.0;
}
else
{
if (grade5 == 'F')
{
grade5num = 0;
}
else
{
cout << "Invalid grade" << endl;
}



gradesum = grade1num + grade2num + grade3num + grade4num + grade5num;

gradeTotal = gradesum/5;


//Output- The total grade points are outputed, followed by the average of
// the grade points, which is the average of the grade points

cout << "Total Grade Points: "<< gradesum << endl;

cout << "GPA :" << gradeTotal;


I am not getting the expected output though. Do I need to use a For Loop so it can check all 5 grades? Can someone help me complete this? Thank you so much!


My expected output is to enter: C,B,A,D,C

and result with:

Total Grade Points: 12
GPA: 2.40

Comments

  • Your program is only testing one grade for letter 'A', another grade for 'B' etc . You need to check each grade for 'A', 'B' , 'C', 'D' and 'F'. The best way to do that and avoid duplication of code (5 sets of if/then/else statements) is to write a function that returns the numeric grade.
    int checkgrade(char g)
    {
      if( g == 'A')
        return 100;
      else if( g == 'B')
    
      etc.
    }
    
  • //try this

    //@dragon functions are above rjh9136, but i do agree

    tempChar = grade1;

    if (tempChar == 'A')
    gradesum = gradesum + 4.0;

    else if (tempChar == 'B')

    ......
  • This would be easier with a loop yes:
    //Christopher D'Angelo (www.mathhead200.com)
    //Use this code ONLY as a guide!
    //Please ask questions if you don't understand something.
    
    int gradeToNum(char grade) {
    	if( grade == 'A' ) return 4;
    	if( grade == 'B' ) return 3;
    	if( grade == 'C' ) return 2;
    	if( grade == 'D' ) return 1;
    	if( grade == 'E' || grade == 'F' ) return 0;
    	return -1; //if no valid grade was found
    }
    
    void kill() {
    	cout << "Bad input!\n";
    	exit(1);
    }
    
    int main() {
    	const int N = 5; //num of grades
    	int sum = 0;
    	char grade; //a place to store each grade
    	//read in the grades one by one, and generates the sum
    	for( int i = 0; i < N; i++ ) {
    		cout << "Enter grade " << i << ": ";
    		if( !(cin >> grade) )
    			kill();
    		int num = gradeToNum( grade );
    		if( num < 0 )
    			kill();
    		sum += num;
    	}
    	cout << "GPA = " << (sum / N) << endl;
    	return 0;
    }
    
  • You have to do this for every grade:
    if (grade1 == 'A')
    {
    grade1num = 4.0;
    }
    
    else
    {
    
    if (grade1 == 'B')
    {
    grade1num = 3.0;
    }
    else
    {
    
    if (grade1 == 'C')
    {
    grade1num = 2.0;
    }
    else
    {
    if (grade1 == 'D')
    {
    grade1num = 2.0;
    }
    else
    {
    if (grade1 == 'F')
    {
    grade1num = 0;
    }
    else
    {
    cout << "Invalid grade" << endl;
    }
    
Sign In or Register to comment.