Home > Blockchain >  Having trouble listing all lettergrades for students in output
Having trouble listing all lettergrades for students in output

Time:11-29

I'm building a program for my computer science 101 class that will ask the user for the student's name and the student's 4 test scores. After this, the program will calculate the average of each student, and based on this average, assign a letter grade for each as well. Then, the program will output each of the student's names, the averages for each student, and the student's letter grade they have earned. After building this, I realized that the letter grade does not always show when outputted.

#include <iostream>
#include <math.h>
using namespace std;


int main(int argc, const char * argv[]) {
    
    //defining variables
    
    float test1 [5];
    float test2 [5];
    float test3 [5];
    float test4 [5];
    float averages[5];
    string lettergrades[5];
    string students [5];
    
    //entering student name and four test scores
    
    cout << "Enter student 1's name: ";
    cin >> students[0];
    cout << "Enter " << students[0] << "'s four test scores: ";
    cin >> test1[0]; cin >> test2[0]; cin >> test3[0]; cin >> test4[0];
    
    cout << "Enter student 2's name: ";
    cin >> students[1];
    cout << "Enter " << students[1] << "'s four test scores: ";
    cin >> test1[1]; cin >> test2[1]; cin >> test3[1]; cin >> test4[1];
    
    cout << "Enter student 3's name: ";
    cin >> students[2];
    cout << "Enter " << students[2] << "'s four test scores: ";
    cin >> test1[2]; cin >> test2[2]; cin >> test3[2]; cin >> test4[2];
    
    cout << "Enter student 4's name: ";
    cin >> students[3];
    cout << "Enter " << students[3] << "'s four test scores: ";
    cin >> test1[3]; cin >> test2[3]; cin >> test3[3]; cin >> test4[3];
    
    cout << "Enter student 5's name: ";
    cin >> students[4];
    cout << "Enter " << students[4] << "'s four test scores: ";
    cin >> test1[4]; cin >> test2[4]; cin >> test3[4]; cin >> test4[4];
    
    //calculating the average for each students' test scores

    averages[0] = (test1[0]   test2[0]   test3 [0]   test4 [0]) / 4;
    averages[1] = (test1[1]   test2[1]   test3 [1]   test4 [1]) / 4;
    averages[2] = (test1[2]   test2[2]   test3 [2]   test4 [2]) / 4;
    averages[3] = (test1[3]   test2[3]   test3 [3]   test4 [3]) / 4;
    averages[4] = (test1[4]   test2[4]   test3 [4]   test4 [4]) / 4;

    
    //calculating the lettergrade for each student
    
    if (averages[0] <= 59.9) {
        lettergrades[0] = " F ";
    } else {
        if (69.9 >= averages[0] > 60.0) {
            lettergrades[0] = " D ";
        } else {
            if (79.9 >= averages[0] > 70.0) {
                lettergrades[0] = " C ";
            } else {
                if (89.9 >= averages[0] > 80.0) {
                    lettergrades[0] = " B ";
                } else {
                    if (averages[0] > 90.0 ) {
                        lettergrades[0] = " A ";
                    }
                }
            }
        }
    }

    if (averages[1] <= 59.9) {
        lettergrades[1] = " F ";
    } else {
        if (69.9 >= averages[1] > 60.0) {
            lettergrades[1] = " D ";
        } else {
            if (79.9 >= averages[1] > 70.0) {
                lettergrades[1] = " C ";
            } else {
                if (89.9 >= averages[1] > 80.0) {
                    lettergrades[1] = " B ";
                } else {
                    if (averages[1] > 90.0 ) {
                        lettergrades[1] = " A ";
                    }
                }
            }
        }
    }
    
    if (averages[2] <= 59.9) {
        lettergrades[2] = " F ";
    } else {
        if (69.9 >= averages[2] > 60.0) {
            lettergrades[2] = " D ";
        } else {
            if (79.9 >= averages[2] > 70.0) {
                lettergrades[2] = " C ";
            } else {
                if (89.9 >= averages[2] > 80.0) {
                    lettergrades[2] = " B ";
                } else {
                    if (averages[2] > 90.0 ) {
                        lettergrades[2] = " A ";
                    }
                }
            }
        }
    }
    
    if (averages[3] <= 59.9) {
        lettergrades[3] = " F ";
    } else {
        if (69.9 >= averages[3] > 60.0) {
            lettergrades[3] = " D ";
        } else {
            if (79.9 >= averages[3] > 70.0) {
                lettergrades[3] = " C ";
            } else {
                if (89.9 >= averages[3] > 80.0) {
                    lettergrades[3] = " B ";
                } else {
                    if (averages[3] > 90.0 ) {
                        lettergrades[3] = " A ";
                    }
                }
            }
        }
    }
    
    if (averages[4] <= 59.9) {
        lettergrades[4] = " F ";
    } else {
        if (69.9 >= averages[4] > 60.0) {
            lettergrades[4] = " D ";
        } else {
            if (79.9 >= averages[4] > 70.0) {
                lettergrades[4] = " C ";
            } else {
                if (89.9 >= averages[4] > 80.0) {
                    lettergrades[4] = " B ";
                } else {
                    if (averages[4] > 90.0 ) {
                        lettergrades[4] = " A ";
                    }
                }
            }
        }
    }

    //displaying the information to the user, in NAME, AVERAGE, LETTERGRADE format
    
    cout << " " << endl;
    cout << students[0] << ": " << averages[0] << "," << lettergrades[0] << endl;
    cout << students[1] << ": " << averages[1] << "," << lettergrades[1] << endl;
    cout << students[2] << ": " << averages[2] << "," << lettergrades[2] << endl;
    cout << students[3] << ": " << averages[3] << "," << lettergrades[3] << endl;
    cout << students[4] << ": " << averages[4] << "," << lettergrades[4] << endl;
    
    return 0;
}

sample output

I've tried rewriting the if else statements, but cannot seem to always guarantee a letter grade next to the student's name. Any help to this problem would be greatly appreciated. Thank you all in advanced!

CodePudding user response:

There is massive scope for typos with all this code duplication. Consider using loops.

In this case, one major problem is you don't understand operators. The statement 69.9 >= averages[2] > 60.0 does not do what you think.

You are also assuming that 69.9 is the largest number that is less than 70. That's not true. What about 69.99 and any of the many other values between 69.9 and 70? Do not test in the range like this. You already have a ladder of tests, so you can arrange it like this:

if (averages[0] < 60.0)
    lettergrades[0] = " F ";
else if (averages[0] < 70.0)
    lettergrades[0] = " D ";
else if (averages[0] < 80.0)
    lettergrades[0] = " C ";
else if (averages[0] < 90.0)
    lettergrades[0] = " B ";
else
    lettergrades[0] = " A ";

Now, you can roll that into a loop to handle all your data with the same logic:

for (int i = 0; i < 5; i  )
{
    if (averages[i] < 60)
        lettergrades[i] = " F ";
    else if (averages[i] < 70.0)
        lettergrades[i] = " D ";
    else if (averages[i] < 80.0)
        lettergrades[i] = " C ";
    else if (averages[i] < 90.0)
        lettergrades[i] = " B ";
    else
        lettergrades[i] = " A ";
}

In a similar way, you can use loops everywhere else that you're duplicating code. That is: inputting scores, calculating averages, and outputting results.

You can even make your grade test cleaner by storing grades and their cutoff value in a table, then search that table.

  •  Tags:  
  • c
  • Related