Link to home
Start Free TrialLog in
Avatar of Tabris42
Tabris42Flag for United States of America

asked on

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!

SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tabris42

ASKER

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.

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
That's what I meant
That did it. Thanks guys!
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.