1

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.

The Problem

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:

enter image description here

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?)

1
  • It seems like there must be another pattern that can model what you're doing here better than long stretches of nested switch statements. Commented Feb 3, 2018 at 2:05

2 Answers 2

5

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
}
1
  • Awesome, thanks a lot for helping me out here. I've tried so many other things to get it resolved, like making separate functions for these switches and finally went to stackoverflow to seek for an answer. Thanks again!
    – J. Doe
    Commented Feb 3, 2018 at 2:09
3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.