-1

so I'm having trouble figuring out what my issue is with it the output needs to be something like "Result 1: 55.00 Grade P"

I'm a beginner so any help would be highly appreciated during this time of struggle, thanks

for(int a=0;a< 10;a++)
{
cout << "Result " << a+1 << " " << score[a] << " " << determine_grade(score[a]) <<  "\n";
}
system("Pause");


}



char determine_grade(double a)
{
char grade;
switch(grade)
{
    case(1):
        if (a >100)
            cout << "Invalid Input!";
    case(2):
        if (a >= 90)
            grade = 'A';
        break;
    case(3):
        if (a >= 70)
            grade = 'B';
        break;
    case(4):
        if (a >= 60)
            grade = 'C';
        break;
    case(5):
        if (a >= 50)
            grade = 'P';
        break;
    default: grade = 'U';
}

cout << grade;
}
confuseduser
  • 23
  • 1
  • 1
  • 4
  • 1
    Should be `case('1')`.. and so on. `grade` is a `char`, not an `int`. Furthermore, it has garbage value since it's not initialized. – Maroun Nov 10 '13 at 08:39

2 Answers2

3

First of all, you're using switch incorrectly: grade is not a valid selector, it's not even initialized. Rewrite the block using only if-else.

Second: in the function, you should return grade;, not cout it. That's the whole point: a function computes some value, and the calling function decides what to do with it (print it or store it or something else).

nullptr
  • 11,008
  • 1
  • 23
  • 18
2

Try this

char determine_grade(double a)
{
    if (a > 100)
    {
        cout << "Invalid Input!";
        return '?';
    }
    if (a >= 90)
       return 'A';
    if (a >= 70)
       return 'B';
    ...
    return 'U';
}

No idea why you are using a switch statement.

The way your code is written determine_grade's job is to return a value which will then be printed in the calling code (i.e. the for loop). So use return in determine_grade not cout.

john
  • 85,011
  • 4
  • 57
  • 81