Switches

I'm having a problem understanding how switches work, I understand their like if statements, but I can't figure out how to get them to work properly.

When I enter a grade, it doesn't print out what I put in the cout

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
  switch (grade)
	{
	case 1:
		grade == 100;
		cout << "You got an A+!" << endl;
		break;

	case 2:
		grade >= 90 && grade <= 99;
		cout << "You got an A!" << endl;
		break;

	case 3:
		grade >= 80 && grade <= 89;
		cout << "You got a B" << endl;
		break;

	case 4:
		grade >= 70 && grade <= 79;
		cout << "You got a C" << endl;
		break;

	case 5:
		grade >= 60 && grade <= 69;
		cout << "You got a D" << endl;
		break;

	case 6:
		grade <= 59;
		cout << "You got a F" << endl;
	}
At line 1, grade must be a value from 1 to 6. The generated code will then execute the statements identified by the case label corresponding to that value.

Your statements at lines 4,9,14,19,24,29 don't do anything because they are simply conditions that are evaluated and the results of that evaluation ignored. These statements have nothing to do with the switch statement.
Last edited on
so then how do you make it so it doesn't evaluate the line but choose the line to output the result? I tried putting if statements inside the cases and it didn't do anything either.

how does the computer know what case it needs to pick without the conditions?
Hey!

Cases are used when you have to perform a simple (if) task but it's a long process. In your case, you shouldn't really use a switch case, I'd rather use an if statement because you have to check multiple things.

Where it says case 1: the '1' will be executed if the integer is '1'. If it's not '1' the switch statement will keep going until it finds something which matches. It is also good to include default: at the end of the cases just in case nothing matches.

Try this:
Ask for a user input from 1 to 5 and create a switch case starting from 1 to 5, and output something else depending on the case.
Last edited on
how does the computer know what case it needs to pick without the conditions?

The value of the variable in the switch() clause determines which branch is chosen.
1
2
3
4
5
6
7
8
9
10
11
12
13
int var = 1;
switch (var)
	{
	case 1:    cout << "The value of var is 1" << endl;
		break;

	case 2:    	cout << "The value of var is 2" << endl;
		break;

	case 3:    cout << "The value of var is 3" << endl;
		break;
                 //  etc...
	} 

It makes so much more sense now, but I'll tell you what I am trying to do, I'm just doing practice programs to get the hang of basic stuff and I'm basing what I do off of this

http://www.cplusplus.com/forum/articles/12974/

For the first program, he says to trying using if statements or switch statements. So I was trying to switch statement. I personally think if statements would work better too but I just want to know how I could run it with a switch
One way to do this with switch/cases is to use integer division on the numeric grade, assuming 100 will be the highest grade. The default case in a switch statement catches everything that wasn't matches in one of the named cases.

1
2
3
4
5
6
7
8
9
switch (grade/10)
{
	case 10:	cout << "You got an A+!" << endl;
			break;

// case 9 through 6 for grades A through D

	default:	cout << "You got an F" << endl;
			break;
Last edited on
Is there a way to do it like

1
2
3
4
5
6
7
8
9
switch (grade)
{
case (100):
cout << "You got an A+!" << endl;
break;

case (>= 90 && <= 99):
cout << You got an A!" << endl;
break; 


Just so the user can enter the actual grade they got instead of 1-10?
You can start the case statements by any number. You don't really need to put (100). Just:

case 100:
//will execute this case if the integer is 100
Last edited on
The user doesn't have to enter 1-10. That's the result you get from dividing the grade they did enter by 10. For example - if they enter 95, 95/10 is 9. If they enter 91, 91/10 is still 9. So those both fall into the same case where they get an A.


Edit:

If you wanted to use a range, that would go into an if/else statement structure.

1
2
3
4
5
6
7
8
9
10
11
12
	if (grade==100)
	       cout << "A+";
	else if (grade>=90)
		cout << "A";
	else if (grade>=80)
		cout << "B";
	else if (grade>=70)
		cout << "C";
	else if (grade>=60)
		cout << "D";
	else
		cout << "F";
Last edited on
oh oh oh, I see where you are going with it wildblue. Thank you
Topic archived. No new replies allowed.