How to use a switch inside a switch?

Asked

Viewed 3,887 times

1

In case, I can not choose the option of the menu select difficulty, I am doing the switch correctly inside a switch?

#include <stdio.h>
#include <stdlib.h>
#define TENTF 5
#define TENTD 3

int main()

{

    char opcao;
    int nome;

    printf("\n\t\t\t-------------------");
    printf("\n\t\t\t   JOGO DA FORCA");
    printf("\n\t\t\t-------------------");
    printf("\n\n\t\t\t1 - INICIAR PARTIDA");
    printf("\n\t\t\t2 - CONFIGURAR DIFICULDADE");
    printf("\n\t\t\t3 - SAIR");
    printf("\n\n\t\t\tOPCAO DESEJADA:");
    scanf("%c", &opcao);

    switch (opcao)
    {
        case '1':
            printf("\n\n\t\t\tINICIAR PARTIDA");
            break;

        case '2':
            printf("\n\n\t\t\tCONFIGURAR DIFICULDADE"); 
            printf("\n\n\t\t\t-------------------");
            printf("\n\t\t\t   JOGO DA FORCA");
            printf("\n\t\t\t-------------------");
            printf("\n\n\t\t\t F - FACIL");
            printf("\n\n\t\t\t D - DIFICIL");
            printf("\n\n\n\t\t\t V - VOLTAR");
            printf("\n\n\t\t\tOPCAO DESEJADA:");
            scanf("%c", &opcao);
            break;

            switch (opcao)

            {
                case 'F':
                    printf("\n\n\t\t\tCONFIGURAR DIFICULDADE"); 
                    printf("\n\t\t\t-------------------");
                    printf("\n\t\t\t   JOGO DA FORCA");
                    printf("\n\t\t\t-------------------");
                    printf("\n\n\t\t\t F - FACIL*");
                    printf("\n\n\t\t\t D - DIFICIL");
                    printf("\n\n\n\t\t\t V - VOLTAR");



                case 'D':
                    printf("\n\n\t\t\tCONFIGURAR DIFICULDADE"); 
                    printf("\n\t\t\t-------------------");
                    printf("\n\t\t\t   JOGO DA FORCA");
                    printf("\n\t\t\t-------------------");
                    printf("\n\n\t\t\t F - FACIL");
                    printf("\n\n\t\t\t D - DIFICIL*");
                    printf("\n\n\n\t\t\t V - VOLTAR");
                    break;

                case 'V':

                    printf("\n\t\t\t-------------------");
                    printf("\n\t\t\t   JOGO DA FORCA");
                    printf("\n\t\t\t-------------------");
                    printf("\n\n\t\t\t1 - INICIAR PARTIDA");
                    printf("\n\t\t\t2 - CONFIGURAR DIFICULDADE");
                    printf("\n\t\t\t3 - SAIR");
                    printf("\n\n\t\t\tOPCAO DESEJADA:");
                    scanf("%c", &opcao);
                    break;

                    }




        case '3':

            printf("\n\n\t\t\tSAIR");
            break;

        default:

            printf("\n\n\t\t\tDESCULPE, A OPCAO DIGITADA EH INVALIDA.");
            printf("\n\n\t\t\tOPCAO DESEJADA:");
            scanf("%c", &opcao);
            break;

    }

}

2 answers

1

Try to put the second switch before break.

case '2':
    printf("\n\n\t\t\tCONFIGURAR DIFICULDADE"); 
    printf("\n\n\t\t\t-------------------");
    printf("\n\t\t\t   JOGO DA FORCA");
    printf("\n\t\t\t-------------------");
    printf("\n\n\t\t\t F - FACIL");
    printf("\n\n\t\t\t D - DIFICIL");
    printf("\n\n\n\t\t\t V - VOLTAR");
    printf("\n\n\t\t\tOPCAO DESEJADA:");
    scanf("%c", &opcao);


    switch (opcao)
    {
        //cases...
    }

    break;
  • same thing, had already tried so...I did with if , It even, but it did not work

  • Boy, check it out right there, 'cause I really don’t see why it doesn’t work that way here. Take all these printf, put something cleaner just to test.

  • Have you tried declaring another variable to capture the second option instead of using it?

0

If you go that way, soon this switch will be a book :)

Use functions instead of sub-switches.

type before main function define:

void nomeDoQueFazNoSubSwitch() { 
    <coloque aqui o sub-switch>
};

and call it that before break:?

nomeDoQueFazNoSubSwitch();
  • in the case I create a subroutine, for example: void difficulty(easy), within this void I create the menu with the selected facil. Ai la no int main() in case '2', option 'F' I call difficulty()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.