Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with looping menu

Posted on 2003-11-03
8
255 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 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 250 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 45

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Accepted Solution

by:
rstaveley earned 250 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to organize data in excel ? 2 115
C++ vs C compilers 13 160
C hashtable library 3 100
C++ finding a sting in a char* string from a text file 3 120
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

838 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