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.
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;
}
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;