how would i change the else if statement below into a switch statement


if (membership != 'S' && membership != 'P' && membership != 'E')
cout << "Please enter S(tandard) or P(lus) or E for Premium. " << endl;
else
{
if (membership == 'S' && purchase <= 75)
reward = purchase * REW_PT1;
else if (membership == 'S' && purchase < 149.99)
reward = purchase * REW_PT2;
else if (membership == 'S' && purchase >= 150)
reward = purchase * REW_PT3;
else if (membership == 'P' && purchase < 150)
reward = purchase * REW_PT4;
else if (membership == 'P' && purchase >= 150)
reward = purchase * REW_PT5;
else if (membership == 'E' && purchase < 200)
reward = purchase * REW_PT6;
else if (membership == 'E' && purchase >= 200)
reward = purchase * REW_PT7;

}

Thx for the help.
Last edited on
You could put switch statements inside of a switch statement.
Or if statements inside of a switch. Kind of like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (membership) {
	case 'S':
		if(purchase <= 75) ...
		else if (purchase > 75 && purchase < 149.99) ...
		else if (purchase >= 150) ...
		break;
	case 'P':
		cout << "p";
		break;
	case 'E':
		cout << "e";
		break;
	default: 
		break;
	}


It starts to get messy looking either way. I think what you are doing with the else if statements looks best.
Topic archived. No new replies allowed.