Need answer by tonight! Switch Statement CAN NOT have breaks!

I'm needing to write a program that gives the user a menu to choose from.
He or she enters a letter for the package they would like. I got the program working great with 'break;' statements. However, the catch is that I'm not allowed to use 'break;' statements, and now I'm entirely stuck. All of the code executes with (a) down without a break, and it's not a linear question, the question asks what kind of car the user would like to buy, so I'm totally stuck as to how to get the same result without 'break;' statements!

(a) Package A
(b) Package B
(c) Package C
(d) Package D

Please help! I need an answer by tonight!

If each package is a subset of the next package you can easily do it without break. What are the parts of each package?
Yes, D is a subset of S, L is a subset of D, P is a subset of L, and B is a subset of P.

I used different letters in my question, but here's what I'm actually dealing with.

The car comes with these different option packages:

Option 'B' is a basic package with the basic features and nothing special.
Option ‘P’ includes, auto transmission, power windows and locks, stereo sound system. cost: base + 1200
Option ‘L’ includes all of the above plus MP3 player, security alarm, cruise control. cost: 600 more than ‘P’
Option ‘D’ includes all of the above plus deluxe detailing, pin stripes, leather seats, cost: 500 more than ‘L’
Option ‘S’ include all the above plus seat heaters, Bose speakers, On Star, steering wheel controls of music system, chrome rims. cost: 800 more than ‘D’

Easy enough. Just put the basic set ups at the bottom, and let the more expensive packages run down into them.
You have that backwards =P

EDIT: nevermind you have it right, I just read what you said wrong.


The idea is a switch statement keeps going until it his a break... so if there's no breaks, it'll keep going (even through other case labels.

For example:

1
2
3
4
5
6
7
8
9
int i = whatever;
switch(i)
{
case 0:
  foo();  // will execute if i==0

case 1:
  bar();  // will execute if i==0 or i==1
}


The 'case 0' bit doesn't stop at the case 1 label because there's no break.
Last edited on
The only problem is, when I do that, the more expensive packages execute all of the code below.

I.e., choosing 'S' executes ALL of this, when I just want one block to execute, but I'm not supposed to use breaks.

case 's':
case 'S':
cout << "This package includes... blah blah blah blah.";
cost = 16800 + 1200 + 600 + 500 + 800;
case 'd':
case 'D':
cout << "This package includes... blah blah blah.";
cost = 16800 + 1200 + 600 + 500;
case 'l':
case 'L':
cout << "This package includes... blah blah.";
cost = 16800 + 1200 + 600;
case 'p':
case 'P':
cout << "This package includes... blah.";
cost = 16800 + 1200;
case 'b':
case 'B':
cout << "This package includes... basic stuff.";
cost = 16800;
default:
Invalid choice. Please restart the program and select again.

Anyway to solve this without 'if' statements within the switch statement?
Basically, put the biggest case at the top. Then, going in order of size, list the other cases after them, with no breaks.
1
2
3
4
5
6
7
8
switch (pack)
{
    case 0:
    cout << "Deluxe features"; // they got deluxe features
    case 1:
    cout << "Basic features"; // but everybody gets basic features
// the output will always show basic features, but it might also show deluxe
}

@Dicsh... I think zhuge has it the right way around.
@OP: You misunderstand. Each package should list only the things that are introduced in them. So you list everything OTHER THAN "and all the previous package".
Last edited on
The only problem is, when I do that, the more expensive packages execute all of the code below.


That's not a problem... that's kind of the point.

Instead of thinking in absolutes... think in terms of building from a common base.

ie: you're thinking like this:


if I have package X, that means the cost is Y and it has Z features


instead, try to think like this:


if I have package X, that means I add Y to the overall cost and include Z features
Ah, the problem was with how I was thinking. I just reworded it to say:

Your purchase includes the following:

Item 1
Item 2
Item 3

and it looks gorgeous now if I do say so myself.

Thank you C++ forum!
Now the only problem is, how to make the default case not say the input is invalid when the valid inputs are given by the user.

Default is supposed to be: do these if expr != any above.

But I require an 'if' ( userInput != 's' || 'S' && userInput != 'd' || 'D') etc. to make that work properly inside the default. Weird.
You can't have a useful default if you can't use break. You'll have to check for valid input before getting to the switch.
1
2
3
4
5
6
7
8
9
10
11
int cost = 0;
switch( package )
{
 case ...:
  cost += whatever;
}

if(cost == 0)
{
  // didn't select a valid package
}
Fixed everything.

Thank you all so much, you all are god sends.
Topic archived. No new replies allowed.