Switch

I have a bit of code but I dunno if it will work in C++. I found it on the PHP website as I found they are quite similar, and just wondered if it would work. It doesn't.

Could you tell me why? And what wuld be a better way of doing it?

1
2
3
4
5
6
7
8
9
switch(true)
{
    case((GameBoard[1][1] == 88) && (GameBoard[1][1] == 88) && (GameBoard[1][1] == 88)):
    case((GameBoard[1][1] == 88) && (GameBoard[1][1] == 88) && (GameBoard[1][1] == 88)):
    case((GameBoard[1][1] == 88) && (GameBoard[1][1] == 88) && (GameBoard[1][1] == 88)):
        Win = true;
        cout << "You win!";
        break;
}


I get this error:

error C2051: case expression not constant
The compiler error is pretty explicit. you can only have integral constants as cases.
For example:
case 1:
case 13:
case 'f':
So if I wanted the same bit of code for each bit, I would have to use an if statement and copy the code for each part?
Right.
Oh fun. :(
Not sure if I'm missing anything, but all three of those case statements look identical to me.

Not only that, but all three of the equality comparisons in each case statement are identical as well.
>> JRASKELL
i noticed that,

just use an if else statement, im lost on the switch(true) also, someone explain that to me.
1
2
3
4
if(GameBoard[1][1] == 88)
{win = true;
cout<<"you win";
}


i doubt that is what your trying to do, you probably forgot to switch your rows & coloums.
Topic archived. No new replies allowed.