Instructor requests inappropriate switch statement application>?

Okay so he wants me to rewrite this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int score;
cout << "Enter your score: ";
cin >> score;

if(score >= 60 && score < 70) 
cout << "D" << endl;
else if(score >= 70 && score < 80)
cout << "C" << endl;
else if(score >= 80 && score < 90)
cout << "B" << endl;
else if(score >= 90)
cout << "A" << endl;
else 
cout << "U" << endl;

system("pause");

return 0;


(simple enough)

"Replace the if-else if-...-else with switch. This step has a little challenge!

Okay, now that's great and all, but I can't figure out how to design a switch statement to accept a range of values- they don't seem to be for that at all dammit but I have to do what he asks. What's killing me is he says replace. I could just make each range output 1, 2, 3, 4, 5. Then tack a switch statement on. But then, I wouldn't have replaced the if...else, only added to it needlessly.

This class is dumb, sigh.
For clarity, this is the same thing using a completely unnecessary and backwards switch statement to handle the output.

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
if(score >= 60 && score < 70) 
grade = 'D';
else if(score >= 70 && score < 80)
grade = 'C';
else if(score >= 80 && score < 90)
grade = 'B';
else if(score >= 90)
grade = 'A';
else 
grade = 'U';

switch(grade)
{
case 'A':
		cout << "You got an A.\n";
		break;
case 'B':
		cout << "You got a B.\n";
		break;
case 'C':
		cout << "You got a C.\n";
		break;
case 'D':
		cout << "You got a D.\n";
		break;
case 'U':
		cout << "You didn't pass.\n";
		break;
}


I'm about to write a program that replaces the if...else structure with a switch statement

All 100 bloody cases. Argh.
To accomplish what your professor is asking you to do, I highly recommend doing the following:
1
2
3
4
5
6
7
8
9
switch(score / 10)
{
case 0:
case 1:
.
.
.
case 10:
}


I am sure you can figure out what needs to be done from there.
Last edited on
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
switch(score)
{
    case –2147483648:
    case –2147483647:
    case –2147483646:
    //...
    case 0:
    case 1:
    case 2:
    //...
    case 58:
    case 59:
        cout << "U" << endl;
        break;

    case 60:
    case 61:
    //...
    case 69:
        cout << "D" << endl;
        break;

    case 70:
    case 71:
    //...
    case 79:
        cout << "C" << endl;
        break;

    case 80:
    case 81:
    //...
    case 89:
        cout << "B" << endl;
        break;

    default:
        cout << "A" << endl;
        break;
}


You'll want to write a program to generate all the cases for the else branch. Hopefully your instructor will realize they're in error.
Last edited on
Thank you Maese, that was a real "aha" moment. Surely this is what he was hoping for.

Shacktar, that's what I was thinking I would do just do be a dick.

Much obliged.
Well, gcc provides an extension for using ranges in switches. Then again, as you have already said yourself that's not what switches are for (well, I personally think that in 99% of all cases in which switches are used it's due to laziness of the programmer rather than an actual need anyways).
Switches seem useful for building menus since they have a limited scope by definition. Other than that, I dunno.
For menus, use polymorphy. Unless it's really simple test code or just some stupid homework you have to hand in.
Topic archived. No new replies allowed.