-8

I have this code. I need it to allow a user to enter five grades as percentages, find the average of them, and display the corresponding letter grade. it also needs to use the functions which I have declared. For some reason, when the program has executed, all of the letter grades are the same, even if the percentages are very different. Can some one please tell me what I might need to edit for the correct letter grade to show up?

#include <iostream>
#include <string>

using namespace std;

float calculateAverage(float math, float english, float science, float history, float geography);
std::string calculateLetterGrade(float grade);

int main()
{

    float math = 0;
    float english = 0;
    float science = 0;
    float history = 0;
    float geography = 0;

    std::cout << "Enter math grade:\n";
    std::cin >> math;
    std::cout << "Enter english grade:\n";
    std::cin >> english;
    std::cout << "Enter science grade:\n";
    std::cin >> science;
    std::cout << "Enter history grade:\n";
    std::cin >> history;
    std::cout << "Enter geography grade:\n";
    std::cin >> geography;

    float sum = (math + english + science + history + geography);
    float Average = sum / 5;

    float grade = (math, english, science, history, geography);
    std::string LetterGrade;

    if (grade >= 90)
    {
    LetterGrade = "A+";
    }
    else if (grade < 90 && grade >= 85)
    {
    LetterGrade = "A";
    }
    else if (grade < 85 && grade >= 80)
    {
    LetterGrade = "A-";
    }
    else if (grade < 80 && grade >= 77)
    {
    LetterGrade = "B+";
    }
    else if (grade < 77 && grade >= 74)
    {
    LetterGrade = "B";
    }
    else if (grade < 74 && grade >= 70)
    {
    LetterGrade = "B-";
    }
    else if (grade < 70 && grade >= 67)
    {
    LetterGrade = "C+";
    }
    else if (grade < 67 && grade >= 64)
    {
    LetterGrade = "C";
    }
    else if (grade < 64 && grade >= 60)
    {
    LetterGrade = "C-";
    }
    else if (grade < 60 && grade >= 57)
    {
    LetterGrade = "D+";
    }
    else if (grade < 57 && grade >= 54)
    {
    LetterGrade = "D";
    }
    else if (grade < 54 && grade >= 50)
    {
    LetterGrade = "D-";
    }
    else
    {
    LetterGrade = "F";
    }

    {
    grade = math;
    std::cout << "Math: " << math << " " << LetterGrade << std::endl;
    }

    {
    grade = english;
    std::cout << "English: " << english << " " << LetterGrade << std::endl;
    }

    {
    grade = science;
    std::cout << "Science: " << science << " " << LetterGrade << std::endl;
    }

    {
    grade = history;
    std::cout << "History: " << history << " " << LetterGrade << std::endl;
    }

    {
    grade = geography;
    std::cout << "Geography: " << geography << " " << LetterGrade << std::endl;
    }

    {
    grade = Average;
    std::cout << "Average: " << Average << " " << LetterGrade << std::endl;
    }

    system("pause");
}
No One
  • 1
  • Can you help me to understand what is that mean ? -> float grade = (math, english, science, history, geography); – HazemGomaa Oct 02 '16 at 04:03
  • Does this even compile? – Falmarri Oct 02 '16 at 04:04
  • float grade = (math, english, science, history, geography); is supposed to be what grade is equal to using the inputs received so that grade can be used to determine the LetterGrade. And yes, it compiles. The wrong same LetterGrade for each subject is the only problem with the code. – No One Oct 02 '16 at 04:06
  • Why do you need the grade variable? Shouldn't you use the Average variable instead? – Jerry Jeremiah Oct 02 '16 at 04:12
  • The grade variable was one of the requirements that I was given. – No One Oct 02 '16 at 04:14
  • @NoOne So do you mean -> float grade = calculateAverage(math, english, science, history, geography); – HazemGomaa Oct 02 '16 at 04:35
  • I just checked that and it didn't compile. From what I understand, float grade cannot equal calculateAverage. – No One Oct 02 '16 at 04:40

1 Answers1

0

float grade = (math, english, science, history, geography); will compile, but it won't do anything useful. More here: How does the Comma Operator work.

My recommendation is to implement std::string calculateLetterGrade(float grade); and have it do the heavy lifting.

So let's do that.

std::string calculateLetterGrade(float grade)
{
    std::string LetterGrade;
    if (grade >= 90)
    {
    LetterGrade = "A+";
    }
    else if (grade < 90 && grade >= 85)
    {
    LetterGrade = "A";
    }
    else if (grade < 85 && grade >= 80)
    {
    LetterGrade = "A-";
    }
    else if (grade < 80 && grade >= 77)
    {
    LetterGrade = "B+";
    }
    else if (grade < 77 && grade >= 74)
    {
    LetterGrade = "B";
    }
    else if (grade < 74 && grade >= 70)
    {
    LetterGrade = "B-";
    }
    else if (grade < 70 && grade >= 67)
    {
    LetterGrade = "C+";
    }
    else if (grade < 67 && grade >= 64)
    {
    LetterGrade = "C";
    }
    else if (grade < 64 && grade >= 60)
    {
    LetterGrade = "C-";
    }
    else if (grade < 60 && grade >= 57)
    {
    LetterGrade = "D+";
    }
    else if (grade < 57 && grade >= 54)
    {
    LetterGrade = "D";
    }
    else if (grade < 54 && grade >= 50)
    {
    LetterGrade = "D-";
    }
    else
    {
    LetterGrade = "F";
    }

    return LetterGrade;
}

There are better ways to do this, but this is readable and obvious what's happening and what the goal is. It also is just a cut and paste of what OP had in the first place so I don't have that horrible feeling that comes from doing someone else's homework for them.

OK. We have a function. So what. What do we do with it?

Something like this:

std::cout << "Math: " << math << " " << calculateLetterGrade(math) << std::endl;
std::cout << "English: " << english << " " << calculateLetterGrade(english) << std::endl;

The most important thing to take from this is your understanding of the C++ language is really, really weak. You're going to have to spend a bit more time with the text book working through problems before the course material gets more advanced and you get overwhelmed. Better to harder run now than have to sprint later.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54