Multipule Switch/Case

I have a question about the switch/case. I have a program that already incorporates the switch/case in it and was wondering if I could put another switch/case in the same program? I am teaching my self how to program by reading books and these types of forums.

Example:

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
switch(product)
		{
			case 'b': 
			{
				cout << "6 pack of Budlight...........$6.49\n"; // If the button b is pushed displays
				Subtotal = Subtotal + Beer; // Calculates the Subtotal and stores it
				Tax = Beer * Taxrate + Tax; // Claculates the total Tax and stores it
			}
			break;
			case 'c':
			{
				cout << "Snickers Bar.................$0.99\n";// If the button c is pusehd displays
				Subtotal = Subtotal + Candy_Bar;
				Tax = Candy_Bar * Taxrate + Tax;
			}
			break;
			case 'm': 
			{
				cout << "1 Gallon of 2% Milk..........$3.99\n";//If the button m is pushed displays
				Subtotal = Subtotal + Milk;
			}
			break;
			case 'r': 
			{
				cout << "Box of Brown Rice............$2.79\n";//If the button r is pushed displays
				Subtotal = Subtotal + Rice;
			}
			break;
			case 'z': Pay++; //When finished it increases pay to 1 to break the while loop
			break;
                }

switch(payment_method)
               {
                        case 'a': //pay with card
                        break;

                        case 'q': //pay with cash
                        break;

                        case 'w': //pay with RFID tag
                        break;
                }
closed account (zb0S216C)
ajaustin12 wrote:
and was wondering if I could put another switch/case in the same program? (sic)

Yes. You could put 70 within the same program if you wanted to, but why you would wan't to do that, I don't know :)P

ajaustin12 wrote:
I am teaching my self how to program by reading books and these types of forums. (sic)

Same.

Wazzak
Last edited on
Yeah you can, there's no issues with that. You can even have a switch case within a switch case if you really wanted to, but it doesn't really look good.

Also, for each of the cases, you don't need brackets. If that case is true, it simply goes through the code below it. And it'll continue on until it reaches "break;" (which causes it to leave the switch) or the end of the switch. The entire switch does need brackets though.

So this would work perfectly fine for one of the cases:
1
2
3
4
5
			case 'b': 
				cout << "6 pack of Budlight...........$6.49\n"; // If the button b is pushed displays
				Subtotal = Subtotal + Beer; // Calculates the Subtotal and stores it
				Tax = Beer * Taxrate + Tax; // Claculates the total Tax and stores it
				break;
Last edited on
Thank you all for your help.

I use the braces because that was the way I was taught (with C) when using multiple statements for clarity purposes.

Each Switch/case is nested inside a while loop until that condition has been met and each switch/case is for a different function.

Again thank you for your help Framework and Pluto is a Planet.
Topic archived. No new replies allowed.