Search code examples
cdo-loops

Nested do while loop


so I've been trying to do a menu with more sub menus. The main menu is a do while loop so if the user tried to enter an irrelevant input, it will let him re-enter. I was wondering if I can do so the same thing with the sub menus but the sub menu loop keeps on going even if I write a valid input. Here is the code:

do
{

    printf("Enter your choice: ");
        scanf(" %d", &choice1);
        switch (choice1)
        {



            case (1) :

             do {
                    printf("4 printfs of choices");


                    printf("Enter your choice: ");
                        scanf(" %d", &choice2);


                switch (choice2)
                        {

                case (1):
                        {
                        stuff
                        }
                    break;

               case (2) :
                       {
                        stuff
                       }
                    break;

              case (3) :
                       {
                        stuff
                       }
                    break;

              case (4) :
                       {
                         exit(0);
                       }

               default :
                       {
                         printf("Sorry that is not a valid choice!\n");
                       }
                       break;
                }
            } while ((choice2 > 0) || (choice2 < 5));
        break;

There is still another while ending for the main loop which is not my concern


Solution

  • I think you want while ((choice2 < 1) || (choice2 > 4));