Help with letter grade to numerical grade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

using namespace std;

int main()
{
    char a;
    int b;
    
    cout << "Please input letter grade: ";
    cin >> a;
    
    if (a == 'A'||a == 'a'){
        b = 1;
    }
    
    if (a == 'B'||a == 'b'){
        b = 2;
    }
    
    if (a == 'C'||a == 'c'){
        b = 3;
    }
    
    if (a == 'D'||a == 'd'){
        b = 4;
    }
    
    if (a == 'F'||a == 'f'){
        b = 5;
    }
    
    else 
        b = 6;
    
    switch (b)
    {
        case 1:
            cout << "Your grade is a 95." << endl;
            break;
        case 2:
            cout << "Your grade is an 85." << endl;
            break;
        case 3:
            cout << "Your grade is a 75." << endl;
            break;
        case 4:
            cout << "Your grade is a 65." << endl;
            break;
        case 5:
            cout << "Your grade is a 50." << endl;
            break;
        case 6:
            cout << "Error!" << endl;
            break;
    }
    
return 0;
}   
Last edited on
It's not
 
if (a == 'A'||'a')


It's

 
if (a == 'A'|| a == 'a')


All of your if statements have this issue.
Alright, I modified it a bit. Every output gives me "Error!" though.
Now the issue is this
1
2
3
4
5
6
if (a == 'F'||a == 'f'){
        b = 5;
    }
    
    else 
        b = 6;


This is saying if the letter was F, b=5, otherwise set B to 6. So for A,B,C,D, it will set B to 6.

You should handle this differently. Remove the following code.
1
2
 else 
        b = 6;


Change your switch statement to the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
switch (b)
    {
        case 1:
            cout << "Your grade is a 95." << endl;
            break;
        case 2:
            cout << "Your grade is an 85." << endl;
            break;
        case 3:
            cout << "Your grade is a 75." << endl;
            break;
        case 4:
            cout << "Your grade is a 65." << endl;
            break;
        case 5:
            cout << "Your grade is a 50." << endl;
            break;
        default:
            cout << "Error!" << endl;
            break;
    }


in your switch/case, default will happen when none of the other cases happen giving you the correct result.
The alternative option is to replace each of your if... if... statements with if ... else if... else if.... You can also replace your switch with putting the statements in the actual if blocks, removing the need for a variable:

1
2
3
4
5
6
7
8
if (a == 'A' || a == 'a') {
    cout << "Your grade is a 95.\n";
} else if (a == 'B' || a == 'b') {
    cout << "Your grade is an 85.\n";
// repeat the above two lines for C, D, F
} else {
    cout << "Error!\n";
}
Thank you! I didn't realize there was a default case for switch statements.
Topic archived. No new replies allowed.