• C

Problem with looping menu

Here is the code for my main menu program:

    while((option = mainmenu()) != 8){
         switch(option){
            case '1':
                break;
            case '2':
                    break;
            case '3':
                break;
            case '4':
                break;
            case '5':
                  break;
            case '6':
                  break;
            case '7':
                  break;
            case '8':
                return 0;
                  break;
            default:
                  printf("\t Please choose an option between 1-8.\n\n");
      break;
           }
    }
    return 0;
}
int mainmenu(){
     int ch;
     printf("\n\t1.  Generate random data\n");
     printf("\t2.  Use pregenerated data\n");
     printf("\t3.  List data\n");
     printf("\t4.  List specific amounts for animal\n");
     printf("\t5.  Determine day most seed was eaten\n");
     printf("\t6.  Generate histogram\n");
     printf("\t7.  Rogue prototype!\n");
     printf("\t8.  Quit\n");
     printf("\t Select an option: ");
     ch = getchar();
     return ch;
}

My problem is that it prints the menu out twice after I've selected a menu option. For example, if I choose option 1 (assume there's code in each of the cases), it will execute the code and then go back to the main menu (printing the menu), then say "Please choose an option between 1-8." and then print the menu again. Then everything will work as normal. How an I make this menu display once, and work each time?

Also, how is it possible to force the user to choose either option 1 or 2 first, before he is allowed to choose options 3-7?
Thanks in advance!

Tabris42Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:

You're getting caught up in the statement:

while((option = mainmenu()) != 8)

The while() statements has to evaluate this every time through the loop, which means that it has to call mainmenu() every time that this is evaluated.  You're going to have to recode the logic here.

Here's your code with a change to the switch() statement that should get you where you're trying to go.


Good Luck!
Kent



    while(1){
      option = mainmenu();
         switch(option){
            case '1':
                return 1;
            case '2':
                   return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                 return 5;
            case '6':
                 return 6;
            case '7':
                 return 7;
            case '8':
                return 0;
            default:
                 printf("\t Please choose an option between 1-8.\n\n");
          }
    }
}

int mainmenu(){
     int ch;
     printf("\n\t1.  Generate random data\n");
     printf("\t2.  Use pregenerated data\n");
     printf("\t3.  List data\n");
     printf("\t4.  List specific amounts for animal\n");
     printf("\t5.  Determine day most seed was eaten\n");
     printf("\t6.  Generate histogram\n");
     printf("\t7.  Rogue prototype!\n");
     printf("\t8.  Quit\n");
     printf("\t Select an option: ");
     ch = getchar();
     return ch;
}
Tabris42Author Commented:
Why the return statements? They will just end my program at every option. I changed the while statement however to
    while(1){
      option = mainmenu();
         switch(option){
         ..etc}}

but still get the same error with the menu repeating twice.
Kent OlsenDBACommented:

The return statement will exit the function, the break statement will exit the switch() statement, but not the while() statement.  Break only exits one level and you need to exit two to get past the test in the while() statement.


Going back to my example, you have two real good ways to keep going once a valid key is pressed.  The first is to do something as soon as the key is pressed:

    while(1){
      option = mainmenu();
         switch(option){
            case '1':
                GenerateRandomData();
                return 1;
            case '2':
                RunWithCurrentData();
                return 2;
            case '3':
                ListData();
                return 3;
            case '4':
                ListAmountsForAnimal();
                return 4;
            case '5':
                FindDayMostSeedEaten()
                 return 5;
            case '6':
                GenerateHistorgram();
                 return 6;
            case '7':
                 /*  etc....  */
                 return 7;
            case '8':
                return 0;
            default:
                 printf("\t Please choose an option between 1-8.\n\n");
          }
    }
}


The function returns a value to indicate which key is pressed.  The calling program can also test the returned value to see what to do:

main()  /*  Main program  */
{
  int option;

  option = GetOption();
  switch(option){
            case '1':
                GenerateRandomData();
                return 1;
            case '2':
                RunWithCurrentData();
                return 2;
            case '3':
                ListData();
                return 3;
            case '4':
                ListAmountsForAnimal();
                return 4;
            case '5':
                FindDayMostSeedEaten()
                 return 5;
            case '6':
                GenerateHistorgram();
                 return 6;
            case '7':
                 /*  etc....  */
                 return 7;
}


Something that looks like one of these solutions is probably what you want.  Since there doesn't appear to be a need for two separate switch statements, try the first of these two solutions.

Kent
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

rstaveleyCommented:
Your input isn't non-canonical, which means that you are required to hit <enter> to flush stdin. You get the menu choice OK, but the next pass gets the new line character.

If you like canonical mode, you can add the line....

   fflush(stdin);

....after your getchar().

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tabris42Author Commented:
Ok, I like that, however, could the reason for the problem be because of a hidden newline character being fed into the switch statement? Therefore making it repeat again with an invalid option.
rstaveleyCommented:
That's what I meant
Tabris42Author Commented:
That did it. Thanks guys!
rstaveleyCommented:
Here's a link showing how to put Linux into non-canonical mode:

http:/Programming/Programming_Languages/Cplusplus/Q_20768726.html#9562567

If you are using Windoze, you might prefer to use getch() instead.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.