Question

Ok guys, I'm having trouble with my switch syntax and I don't know why I can't compile it.

I'm posting the part of the code that's wrong so you can help me.

If you need any information about the code, just ask me.

for (c=0; c<3; c++) // 3 jogadores ( 4 - voce )
{
    switch(c)
    {
        case 0:
            char cartas[7];
            cartas[0] = jog2_carta1[0];
            cartas[1] = jog2_carta1[1];
            cartas[2] = jog2_carta2[0];
            cartas[3] = jog2_carta2[1];
            cartas[4] = jog2_carta3[0];
            cartas[5] = jog2_carta3[1];
            cartas[6] = '\0';

            if (strcmp(retornaMaior(cartas,mesa_jog1[0]), "00") == 1)
            {
               printf("%s", retornaMaior(cartas,mesa_jog1[0]));
               maiorRodada = 2;
               // remove carta (na funcao)
            }
            else
            {
                // remove menor
            }
            break;
        case 1: // jogador 3
            char cartas[7];
            cartas[0] = jog3_carta1[0];
            cartas[1] = jog3_carta1[1];
            cartas[2] = jog3_carta2[0];
            cartas[3] = jog3_carta2[1];
            cartas[4] = jog3_carta3[0];
            cartas[5] = jog3_carta3[1];
            cartas[6] = '\0';

            if (strcmp(retornaMaior(cartas,mesa_jog2[0]), "00") == 1)
            {
                if (maiorRodada == 2)
                {
                    printf("%s", retornaMaior(cartas,mesa_jog2[0]));
                    maiorRodada = 3;
                    // remove carta (na funcao)
                }           
            }
            else
            {
                // remove menor
            }
            break;
        case 2: // jogador 4
            break;
    }
}
Was it helpful?

Solution

First, if you're asking about a syntax error, always show us the exact error message, and clearly indicate which line it refers to.

The problem is that the syntax for a statement with a case label is:

case constant-expression : statement

You have:

case 0:
    char cartas[7];
    cartas[0] = jog2_carta1[0];
    // ...

char cartas[7]; is a declaration, not a statement.

You can solve this by enclosing the body of each case with curly braces, making the entire block of code (including the leading declarations) a single statement:

case 0:
    {
        char cartas[7];
        cartas[0] = jog2_carta1[0];
        // ...
        break;
    }

OTHER TIPS

You might like to open a fresh context for each case statement, like this:

switch(c)
{
  case 0:
  {
     /* Put variable definitons here. */
  }
  case 1:
  {
  }
  ...
  default:
  {
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top