using Switch statement

Feb 25, 2010 at 7:45am
am trying to solve this:
Write a switch statement on the (nonnegative integer) marks m so that it outputs
_ 'H' when m≥85
_ 'D' when 75≤m<85
_ 'C' when 65≤m<75
_ 'P' when 50≤m<65
_ 'F' when m<50

This is my attempt, please help show me where am going wrong

// swich statements continued
#include <iostream>
using namespace std;
int main( )
{
unsigned int m;
string marks ;

cout << "Please enter your marks for grading: ";
cin >> marks ;

switch (marks)
{
case ‘m>=85’:
marks = "H";
switch;

case ‘75<=m<85’
marks = "D";
switch;

case ‘65≤m<75’
marks = "C";
switch;

case ‘50≤m<65’
marks = "P";
switch;

case ‘m<50’
marks = "F";
switch;

}
cout << marks << endl;
return 0;
}//swich2.cpp
Feb 25, 2010 at 1:55pm
You can't do what you want to do with switch() statements unless you want
to have cases for every possible integer between 50 and 85 inclusive.
Feb 25, 2010 at 9:09pm
Thnks smith for the response. So whats your opinion answering the question above
Feb 25, 2010 at 9:27pm
My opinion is that what you want to do is impractical with a switch-case construct.
Feb 25, 2010 at 10:24pm
I really want to help but I'm not sure what you want to do with this program are you trying to make like a grading programing were you enter the number grade and gives you the letter... or are you trying to enter the letter and get the number grade?
Feb 25, 2010 at 11:20pm
@jsmith: Yes you can. You're just not being creative enough :).This isn't practical but most homework assignments aren't either. This is not complete or compiled but here's how.

if(marks >= 85)
{marks = 1;}
... //Repeat for each range test
...
...
switch(marks)
{ case 1:
cout << 'H';
break;}
...
...
... //Ad Nausium
By the way your assignment of a char to an unsigned int in every case statement is illegal, you need a char varialbe if you want to assign a the letter value to anything.
What is thios 'switch;' stuff at the end of your case statements?
Feb 26, 2010 at 12:34am
@OP: You misinterpret switch. The only check switch can perform is == (equality). You cannot compare with other relational operators. This code is invalid:
1
2
3
4
5
6
switch (someinteger)
{
    case someinteger < 5: // does not work, you can only switch on a specific value for someinteger
    break;
    case 5: // this case will be executed if someinteger == 5
}

@computergeek: That would work but it seems a little bit inefficient... I'd rather just use an if statement for this entire problem. You really can't accomplish that in any efficient way with switch, so you may want to check with your teacher that they actually mean switch.
And @OP, I think you mean a break;. Read this:
http://www.Python/doc/tutorial/control/#switch
Last edited on Feb 26, 2010 at 12:35am
Feb 26, 2010 at 3:30am
@Computergeek01:

Come on... I have 3,600 replies here. Don't you think I'm at least that "creative"? Of course
anything is possible. I was limiting my answer to the practical.
Feb 26, 2010 at 3:34am
@Computergeek:

That doesn't even really use a switch anyway. I mean it does... but the switch is redundant.

Why have an if/else chain and a switch. Doesn't using one defeat the point of using the other?
Feb 26, 2010 at 6:45pm
case ‘m>=85’:
marks = "H";
switch;
m>=85 does not work in switch statement
and u r giving switch after every case statement which is not the correct syntax
Topic archived. No new replies allowed.