Mystgun Fairytail
Mystgun Fairytail

Reputation: 11

Want to do a loop, that if i press an option it goes back to another menu

I want the option 2 in menu2, to go back to menu1 and start all over, but when it does, it is stuck when i go to menu 2 and press 2, it goes back to menu 1, but if i press the 1 option it stays in a loop and doesn't advance. I can only use Do...While for this. Can't figure it out alone. I can only use those loops, and just learned to do functions, but not quit good at it. And i'm a beginner, so any tip would be really good.

    main() {

    setlocale(LC_ALL, "portuguese");

    char escolha, dificuldade, modo, order, go_b, restart, dificuldadeteste;            //variaveis a utilizar, teste1 - dividir easy de hard para realizar =/ contadores
    char rf1, rf2, rf3, rf4, rf5, rf6, rf7, rf8, rf9, rf10;
    char ra1, ra2, ra3, ra4, ra5, ra6, ra7, ra8, ra9, ra10;
    int respostas_vdd = 0, respostas_fls = 0, times = 0;            //inicializar para não dar erro please!

     do {                                                   //repeat complete code                      
           do {                                             //repeat menu 2 
                 do{                                        //repeat menu 1
    
    top();
    for(int j=1; j<=1; j++){
    lateral();}
    base();
    
    //menu1****************
    putchar('\n');
    printf("Escolha a opção que deseja. Caso o numero que insira não esteja na lista, o menu será reiniciado.\n");
    printf("1. Selecione o nível de dificuldade que pretende jogar.\n");
    printf("2. Instruções de utilização do quiz.\n");
    printf("3. Sair do programa.\n");
    escolha = getche();
    }while(escolha < '1' || escolha > '3');
        
    //menu2***************
    switch (escolha)                                                                                                                //projeto inicial que vai seguir para o restante modelo
    {
    case '1':
        puts("\nDeseja iniciar no Nivel [F]ácil ou [A]vançado? Prima a 1ª letra correspondente a dificuldade desejada.");
        //scanf(" %c", &dificuldade);
        dificuldade = getche();
        break;
    case '2':
        system("cls");
        printf("\n1. - O jogo consiste num esquema de perguntas as quais o tuilizador deve responder.\n");
        printf("2. - Para escolher a opção, deve colocar o número que a mesma representa.\n");
        printf("3. - No final das respostas, terá uma pontuação que coresponde às suas respostas.\n");
        putchar('\n');
        puts("Prima S para voltar ao menu anterior.\n");                                                                            //falta inserir um system("cls"); para melhor visualização
        restart = getche();
        system("cls");                                                                                                              //opção não válida para o resultado que queria
        break;
    case '3':
        printf("\nSair do programa.\n");
        exit(0);
        break;
    default:
        exit(0);
        break;
    }

    }while (restart == 'S' || restart == 's');                                                                                  //restart do switch escolha **TEM BUG**

    
    switch (dificuldade)                                                                                                            //atenção ao caps
    {
    case 'F':
    case 'f':
        printf("\nO jogo irá iniciar no modo fácil.\n");
        break;
    case 'A':
    case 'a':
        printf("\nO jogo irá iniciar no modo avançado.\n");
        break;
    
    default:
            printf("\nTecla não presente no menu, por favor repita.\n");                                                            //aplicação do while para repetir opção?
            exit(0);
            break;
    }



    if (dificuldade == 'F' || dificuldade == 'f')                                                                                   //code para a escolha de facil
    {
        printf("\nResponda a todas as questões com 'V' ou 'F' até ao fim de todo o jogo. Obrigado, bom jogo. <ENTER>\n");
        getchar();
        system("cls");
        printf("Nível Inicial\n");
        printf("*************\n");
        putchar('\n');
        
        
        puts("\nPokemon Amarelo foi o 1º jogo da franquia? [V] ou [F]");
        do {
        rf1 = getche();
            if (rf1 == 'V' || rf1 == 'v')
            {
                respostas_fls++;
            }
                else if (rf1 == 'F' || rf1 == 'f')
                { 
                    respostas_vdd++;
                }
            else
                printf("\nTecla incorreta, tente novamente.\n");
        }while(rf1 != 'V' && rf1 != 'F' && rf1 != 'v' && rf1 != 'f');                                                           //aceitar apenas as letras fornecidas no quiz

        puts("\nO personagem Principal chama-se Ash? [V] ou [F]");
        do {
        rf2 = getche();
            if (rf2 == 'V' || rf2 == 'v')
            {   
                respostas_vdd++;
            }
                else if (rf2 == 'F' || rf2 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf2 != 'V' && rf2 != 'F' && rf2 != 'v' && rf2 != 'f');

        puts("\nO 1º Pokemon de Ash foi Pikachu? [V] ou [F]");
        do {
        rf3 = getche();
            if (rf3 == 'V' || rf3 == 'v')
            {
                respostas_vdd++;
            }
                else if (rf3 == 'F' || rf3 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf3 != 'V' && rf3 != 'F' && rf3 != 'v' && rf3 != 'f');
        
        puts("\nJennifer é o nome da enfermeira no PokeCenters? [V] ou [F]");
        do{
        rf4 = getche();
            if (rf4 == 'V' || rf4 == 'v')
            {
                respostas_fls++;
            }
                else if (rf4 == 'F' || rf4 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf4 != 'V' && rf4 != 'F' && rf4 != 'v' && rf4 != 'f');
        
        puts("\nO 1º Professor Pokemon que Ash conheceu foi o Oak? [V] ou [F]");
        do{
        rf5 = getche();
            if (rf5 == 'V' || rf5 == 'v')
             {
                respostas_vdd++;
             }
                else if (rf5 == 'F' || rf5 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf5 != 'V' && rf5 != 'F' && rf5 != 'v' && rf5 != 'f');
        
        puts("\nO nome da 1ª região Pokémon era Jhoto. [V] ou [F]");
        do{
        rf6 = getche();
            if (rf6 == 'V' || rf6 == 'v')
            {
               respostas_fls++;
            }
                else if (rf6 == 'F' || rf6 == 'f')
                {
                    respostas_vdd++;
                }   
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf6 != 'V' && rf6 != 'F' && rf6 != 'v' && rf6 != 'f');
        
        puts("\nPikachu evolui com amizade. [V] ou [F]");
        do{
        rf7 = getche();
             if (rf7 == 'V' || rf7 == 'v')
            {
               respostas_fls++;
            }
                else if (rf7 == 'F' || rf7 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf7 != 'V' && rf7 != 'F' && rf7 != 'v' && rf7 != 'f');
        
        puts("\nDragonite é um Pokémon Lendário. [V] ou [F]");
        do{
        rf8 = getche();
            if (rf8 == 'V' || rf8 == 'v')
            {
                respostas_fls++;
            }
                else if (rf8 == 'F' || rf8 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf8 != 'V' && rf8 != 'F' && rf8 != 'v' && rf8 != 'f');
        
        puts("\nMewtwo é uma cópia genética de Mew. [V] ou [F]");
        do{
        rf9 = getche();
            if (rf9 == 'V' || rf9 == 'v')
            {
               respostas_vdd++;
            }
                else if (rf9 == 'F' || rf9 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf9 != 'V' && rf9 != 'F' && rf9 != 'v' && rf9 != 'f');
        
        puts("\nO 1º Pokémon foi Mew. [V] ou [F]");
        do{
        rf10 = getche();
            if (rf10 == 'V' || rf10 == 'v')
            {
                respostas_fls++;
            }
                else if (rf10 == 'F' || rf10 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(rf10 != 'V' && rf10 != 'F' && rf10 != 'v' && rf10 != 'f');
    }

    if (dificuldade == 'A' || dificuldade == 'a')                                                                                               //2º if para a parte avançada do quiz
    {
        printf("Responda a todas as questões com 'V' ou 'F' até ao fim de todo o jogo. Obrigado, bom jogo.");
        system("cls");
        printf("Nível Avançado\n");
        printf("**************\n"); putchar('\n');
        puts("Pokemon Amarelo foi o 1º jogo da franquia? [V] ou [F]");
        do {
        ra1 = getche();
            if (ra1 == 'V' || ra1 == 'v')
            {
             respostas_fls++;
            }
                else if (ra1 == 'F' || ra1 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra1 != 'V' && ra1 != 'F' && ra1 != 'v' && ra1 != 'f');
        
        puts("\nCharizard é solto porque conheceu uma companheira? [V] ou [F]");
        do{
        ra2 = getche();
            if (ra2 == 'V' || ra2 == 'v')
            {
                respostas_vdd++;
            }
                else if (ra2 == 'F' || ra2 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra2 != 'V' && ra2 != 'F' && ra2 != 'v' && ra2 != 'f');
        
        puts("\nAsh tem mais de 100 Tauros em Kanto? [V] ou [F]");
        do {
        ra3 = getche();
            if (ra3 == 'V' || ra3 == 'v')
            {
              respostas_vdd++;
            }
                else if (ra3 == 'F' || ra3 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra3 != 'V' && ra3 != 'F' && ra3 != 'v' && ra3 != 'f');
        
        puts("\nO Meowth do Team Rocket sofreu um desgosto de amor? [V] ou [F]");
        do{
        ra4 = getche();
             if (ra4 == 'V' || ra4 == 'v')
            {
                 respostas_fls++;
            }
                else if (ra4 == 'F' || ra4 == 'f')
                {
                   respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra1 != 'V' && ra1 != 'F' && ra1 != 'v' && ra1 != 'f');
        
        puts("\nBrock aparece na 6ª Temporada? [V] ou [F]");
        do {
        ra5 = getche();
            if (ra5 == 'V' || ra5 == 'v')
            {
               respostas_vdd++;
            }
                else if (ra5 == 'F' || ra5 == 'f')
                {
                   respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra5 != 'V' && ra5 != 'F' && ra5 != 'v' && ra5 != 'f');
        
        puts("\nPokemon Journeys não é a 8ª temporada. [V] ou [F]");
        do {
        ra6 = getche();
            if (ra6 == 'V' || ra6 == 'v')
            {
                respostas_vdd++;
            }
                else if (ra6 == 'F' || ra6 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra6 != 'V' && ra6 != 'F' && ra6 != 'v' && ra6 != 'f');   
         
        puts("\nEvee pode evoluir para todos os tipos de pokemon. [V] ou [F]");
        do {
        ra7 = getche();
            if (ra7 == 'V' || ra7 == 'v')
            {
                respostas_fls++;
            }
                else if (ra7 == 'F' || ra7 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra7 != 'V' && ra7 != 'F' && ra7 != 'v' && ra7 != 'f'); 
           
        puts("\nApenas Pokemon com stats superior a 700 são lendários. [V] ou [F]");
        do{
        ra8 = getche();
            if (ra8 == 'V' || ra8 == 'v')
            {
                respostas_fls++;
            }
                else if (ra8 == 'F' || ra8 == 'f')
                {
                    respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra8 != 'V' && ra8 != 'F' && ra8 != 'v' && ra8 != 'f');    
        
        puts("\nAsh viajou pelo espaço. [V] ou [F]");
        do{
        ra9 = getche();
            if (ra9 == 'V' || ra9 == 'v')
            {
                respostas_vdd++;
            }
                else if (ra9 == 'F' || ra9 == 'f')
                {
                    respostas_fls++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra9 != 'V' && ra9 != 'F' && ra9 != 'v' && ra9 != 'f');    
        
        puts("\nEternatus é o pokémon com mais stats. [V] ou [F]");
        do{
        ra10 = getche();
            if (ra10 == 'V' || ra10 == 'v')
            {
                respostas_fls++;
            }
                else if (ra10 == 'F' || ra10 == 'f')
                {
                   respostas_vdd++;
                }
            else
            printf("\nTecla incorreta, tente novamente.\n");
        }while(ra10 != 'V' && ra10 != 'F' && ra10 != 'v' && ra10 != 'f');
    }


    printf("\nO jogo terminou. Você teve %d respostas certas e %d respostas erradas", respostas_vdd, respostas_fls);
    putchar('\n');
    
    puts("\nDeseja recomçecar? Pressione Y para sim, e qualquer outra tecla para terminar.\n");
    order = getche();
    times++;                                                                                                                                            //inserir contador para o nº de vezes jogados
    system("cls");                                                                                                                                      //limpar o vizor a cada jogo novo
    }while(order=='Y' || order=='y');                                                                                                                   //while para reiniciar todo o code
    
    printf("\nObrigado por ter usado o meu programa!!!! Jogou %d vezes!", times);                                                                       //fim, e contagem de vezes jogadas

    getchar();                                                                                                                                          //<enter> para satisfação
    
    return 0;
    
}

The thing is that, everything does what i want, but if i press for example Menu 1 Option 2, and then Menu 2 Option 2 and Click S. It goes back to Menu 1 as i intended, but then in Menu 1, the option 1 doesn't work, keeps looping. But Option 2 and 3 works fine. I know i need to train more, and the code might be very bad, but it's the only thing i could make for it to work, based on the things I've learned so far. Thank You for all the tips so far.

Upvotes: 0

Views: 99

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409364

In pseudo code you could use functions to represent menus and sub-menus similar to this:

void foo_menu()
{
    // Infinite loop
    for (;;)
    {
        display_foo_menu();

        selection = generic_get_selection();

        switch (selection)
        {
        case BACK_TO_PREVIOUS_MENU:
            return;

        case DO_FOO:
            perform_foo_action();
            break;

        default:
            generic_print_error_message();
            break;
        }
    }
}

void bar_menu()
{
    // Infinite loop
    for (;;)
    {
        display_bar_menu();

        selection = generic_get_selection();

        switch (selection)
        {
        case BACK_TO_PREVIOUS_MENU:
            return;

        case GO_TO_FOO_MENU:
            foo_menu();
            break;

        case DO_BAR:
            perform_bar_action();
            break;

        default:
            generic_print_error_message();
            break;
        }
    }
}

int main()
{
    // Infinite loop
    for (;;)
    {
        display_main_menu();

        selection = generic_get_selection();

        switch (selection)
        {
        case GO_TO_BAR_MENU:
            bar_menu();
            break;

        case DO_SOMETHING:
            perform_something_action();
            break;

        default:
            generic_print_error_message();
            break;
        }
    }
}

When leaving a sub-menu we just return from the function, and the program will resume on that menu. When a sub-menu is wanted, we just call the corresponding function to display and handle it.

This works similar to a hierarchical state machine which is logical if you consider menu alternatives being states.

Upvotes: 1

Related Questions