ophychius
ophychius

Reputation: 2653

Objective-C Using an enum in switch statements, one's working but the other won't

I have a little dilemma here with a piece of code I wrote. I have to determine a facing based on a previous facing and a move.

I made a simple enum for the 4 facings :

typedef enum directions {N,E,S,W}Facing;

And then there are three functions, each working with a simple switch statement, taking an action based on the current facing. Now, the functions turnLeft and turnRight work just fine, but in the function move, the switch statement doesn't hit any of the cases, even though I know for sure either N,E,S, or W are entered.

- (Facing) turnLeft: (Facing) f
{
    switch (f)
    {
        case N:
            f = W;
            break;
        case E:
            f = N;
            break;
        case S:
            f = E;
            break;
        case W:
            f = S;
            break;
        default:
            break;
    }
    return f;
}

- (Facing) turnRight: (Facing) f
{
    switch (f)
    {
        case N:
            f = E;
            break;
        case E:
            f = S;
            break;
        case S:
            f = W;
            break;
        case W:
            f = N;
            break;
        default:
            break;
    }
    return f;
}

- (void) move:(Facing) f
{
    switch (f)
    {
        case N:
            y+1;
            break;
        case W:
            x+1;
            break;
        case S:
            y-1;
            break;
        case E:
            x-1;
            break;
        default:
            break;
    }

}

So, as far as I know, all those switches work simmilarly, yet the third one is not working, the first two work perfectly fine. Does anyone have any idea what could be the problem?

Upvotes: 0

Views: 2374

Answers (1)

Tommy
Tommy

Reputation: 100662

The third switch statement is correct, as are the other two, but I think your problem is that it doesn't actually do anything. You compute y+1, x+1, y-1 or x-1 and then just throw the result away. Are you looking for e.g. x = x+1;?

Upvotes: 1

Related Questions