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;
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?
@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?
@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.cplusplus.com/doc/tutorial/control/#switch
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.
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