Switch Statements

I have looked all over the forum to find out if this switch statement is legal. I have also banged my head on the wall to try and figure this out. Can a switch statement condition be met by a range? According to my instructions I need to create a range and from that range have it go into a case. I have written something similar to what i need. Does the switch statement need a cin for it to work?

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
41
42
43
44
45
46
47
48
49
 #include <cmath>
#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
	char choice;
	double milkprice, darkprice, whiteprice, europeanprice, milk, dark, white, european, subtotal;
	double gross, discount10, discount15, discount20, discount25;

	cout << "\n\nEnter option ";
	cin >> choice;

	if ((choice >= 20) && (choice < 40))
		choice = 1;
	
	if ((choice > 40) && (choice < 60))
		choice = 2;
		
	switch (choice)
	{
			case '1':
			
				cout << "\n\nCongratulations because you purchased more than 20 dollars worth of chocolate ";
				cout << "\n you get a discount of 10% off your order!" ;
				gross = subtotal * discount10;
				subtotal = subtotal - gross;
			
				break;
			
			case '2':
			
				cout << "\n\nCongratulations because you purchased more than 40 dollars worth of choolate";
				cout << "\n you get a discount of 20% off your order!";
				gross = subtotal * discount20;
				subtotal = subtotal - gross;
	}	 
			
			
			
			
		
		
	

	return 0;
}
It should be case 1: and case 2:, not case '1': and case '2':.
long double main thanks for the quick reply. Your suggestion worked however, it just runs case 2 every time now regardless of the choice. Any idea why?
char choice;

If they're entering a number, I'd define choice as an integer.

Edit:

You can't use a range on a switch case, but it doesn't need a cin. Switch is looking at the value of an expression and performing different actions depending on the value.
Last edited on
hey wildblue,

Thanks for the help. I have been trying to do this all day I think my head is getting tired. I had them as "char" earlier and just forgot to convert it over. I understand that the switch case can't be anything but chars or ints for it to go in to its respective case. Thanks for all the help!
Topic archived. No new replies allowed.