Grades Program switch statements

I was wondering what I have wrong here, i need a grade entered then a switch statement to display they got a A or B, etc. The program runs but when the grade is entered nothing is displayed.

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
#include <iostream>
using namespace std;

// Grades Program Switch

int main()
{
    int score;
    
    cout << "Please enter a score: ";
    cin >> score;
    
    switch (score)
    {
        case 'A': (score == 100);
            cout << "\nYou got a perfect score\n";
            break;
        
        case 'B': (score < 99 && score >= 90);
            cout << "\nYou got an A\n";
            break;
            
        case 'C': (score < 90 && score >= 80);
            cout << "\nYou got a B\n";
            break;
            
        case 'D': (score < 80 && score >= 70);
            cout << "\nYou got a C\n";
            break;
            
        case 'E': (score < 70 && score >= 60);
            cout << "\nYou got a D\n";
            break;
            
        case 'F': (score < 60);
            cout << "\nYou got a F\n";
            break;
    }
            
    return 0;
}
Who said you needed a switch statement? The values ascribed to each “case” have to be those for the variable in switch(variable). But score doesn’t take the values A, B, C etc. Your syntax on the lines starting “case” is also non-c++.

Use an if ... else if .. chain, if deciding on the basis of score.
Last edited on
Ok, i see. It was something i was trying to see if it would work with switch statements. I see what your saying.
Hello Reaper1992,

You could use a switch, but it would be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (score)
{
    case 100:
        std::cout << "\nYou got a perfect score\n";
        break;
    case 99:
    case 98:
    case 97:
        cout << "\nYou got an A\n";
        break;
    case 89:

    default:
        break;
}

You would have sections for every range, (99 - 90, 89 - 80) and so on. But then you would have to take the case statements all the way down to (0)zero.

As lastchance says the if/else becomes a better option.

Andy
be creative :)

switch (score/10)
{
case 10: perfect
case 9: A
...
default fail
}

no, it does not work if you have more challenging ranges than 10 / bucket
Last edited on
If you are using GCC as your compiler, you could use (nonstandard extension) case ranges to simplify the code if you are really set on using a switch statment:

https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html
You could also use an array to hold the mark/grade breaks. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <utility>

char grade(int score)
{
	constexpr std::pair<int, char> grades[] {{90, 'A'}, {80, 'B'},  {70, 'C'}, {60, 'D'}};

	for (const auto [mark, grade] : grades)
		if (score >= mark)
			return grade;

	return 'F';
}

int main()
{
	int score {};

	std::cout << "Please enter a score: ";
	std::cin >> score;

	std::cout << "You got " << grade(score) << '\n';
}

Topic archived. No new replies allowed.