• 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?
 
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().
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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;
}
0
 
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.
0
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

 
Kent OlsenData Warehouse Architect / DBACommented:

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
0
 
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.
0
 
rstaveleyCommented:
That's what I meant
0
 
Tabris42Author Commented:
That did it. Thanks guys!
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.