Reputation: 69
I've been developing a project which uses the switch()
function, like so:
switch (selectedMenu) {
case 1:
switch (selectedIndex) {
case 0:
invisibility = !invisibility;
break;
case 1:
invincibility = !invincibility;
break;
case 2:
superjump = !superjump;
break;
case 3:
//Option 4
break;
case 4:
//Option 5
break;
}
case 2:
switch (selectedIndex) {
case 0:
//Option 2
break;
case 1:
//Option 2
break;
case 2:
//Option 3
break;
case 3:
//Option 4
break;
case 4:
//Option 5
break;
}
case 3:
switch (selectedIndex) {
case 0:
//Option 2
break;
case 1:
//
break;
case 2:
//Option 3
break;
case 3:
//Option 4
break;
case 4:
//Option 5
break;
}
case 4:
switch (selectedIndex) {
case 0:
//Option 2
break;
case 1:
//Option 2
break;
case 2:
//Option 3
break;
case 3:
//Option 4
break;
case 4:
//Option 5
break;
}
case 5:
switch (selectedIndex) {
case 0:
//Option 2
break;
case 1:
SET_ENTITY_COORDS(pedID, -75.015, -818.215, 326.176);
break;
case 2:
//Option 3
break;
case 3:
//Option 4
break;
case 4:
//Option 5
break;
}
}
I've encountered a bug with my code.
In case 5 -> case 1
there's this line: SET_ENTITY_COORDS(pedID, -75.015, -818.215, 326.176);
which should teleport a entity to a certain location. When I use an option in any of the cases above this teleport case it will teleport the entity aswell, even though it's not in the case.
It's very hard to explain, so lets make a drawing:
How do I adjust this so it only enables the function I specified in the case and not everything above it aswell?
(How do I adjust this so it only enables superjump instead of both superjump and teleports the user?)
Upvotes: 1
Views: 317
Reputation: 7905
Michael Burr already said what you need to do to fix your problem, but what's happening in your code is that the natural design of switch
statements in both C
& C++
is that if you do not use the conditionals: break
or continue
or return
out of the function from within the case
section the code will fall-through
to the next case
statement.
Simple example:
switch( idx ) {
case 1: {
// some code
}
case 2: {
// some code
continue;
}
case 3: {
// some code
break;
}
case 4: {
// some code
return statement;
}
}
In the above code sample case 1
does not use continue
, break
or return
from the function that this switch statement is in. So what will happen here is if case 1
is encountered it will execute the code within case 1
's code block then it will fall through to check case 2
. Case 2
will continue execution, case 3
will break out of the switch statement, and case 4
will return from the function directly. These same traits of the switch-case statements also apply to nested switch statements.
Upvotes: 3
Reputation: 340218
You need break
statements for your outer switch
cases:
switch (selectedMenu) {
case 1:
switch (selectedIndex) {
// blah, blah, blah...
}
break; // <-- this
case 2:
switch (selectedIndex) {
// blah, blah, blah...
}
break; // <-- this
case 3:
switch (selectedIndex) {
// blah, blah, blah...
}
break; // <-- this
case 4:
switch (selectedIndex) {
// blah, blah, blah...
}
break; // <-- this
case 5:
switch (selectedIndex) {
// blah, blah, blah...
}
break; // <-- this
}
Upvotes: 5