Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with looping menu

Posted on 2003-11-03
8
Medium Priority
?
269 Views
Last Modified: 2010-04-15
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!

0
Comment
Question by:Tabris42
  • 3
  • 3
  • 2
8 Comments
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1000 total points
ID: 9671412

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
 

Author Comment

by:Tabris42
ID: 9671588
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9671726

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Accepted Solution

by:
rstaveley earned 1000 total points
ID: 9671834
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
 

Author Comment

by:Tabris42
ID: 9671874
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9671886
That's what I meant
0
 

Author Comment

by:Tabris42
ID: 9671904
That did it. Thanks guys!
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9671929
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question