How can I repeat a menu on wrong input?

This is a discussion on How can I repeat a menu on wrong input? within the C Programming forums, part of the General Programming Boards category; Would anyone be able to know a way I can make the program display the menu again to the user ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    How can I repeat a menu on wrong input?

    Would anyone be able to know a way I can make the program display the menu again to the user when their input is invalid?
    Any other than copying and pasting the menu and if statements in the else.

    Thanks,

    Code:
    #include <stdio.h>
    #include <process.h>
    #include <conio.h>
    
    int meteorological_module();
    int miscelleneous_module();
    
    int main()
    {
       int reply_menu1 = 0;
       int items_menu1 = 0;
    
       clrscr();
       printf(" This Program is still under development\n\n");
       printf(" Here are the menu options that this program offers: \n\n");
       printf(" %d. Select this option to go to the METEOROLOGICAL modules"
                 " program\n", 1);
       printf(" %d. Select this option to go to the MISCELLANEOUS modules"
                 " program\n", 2);
       printf(" %d. Enter three or a higher number to exit from this"
                 " program\n", 3);
       printf(" Choose your option: ");
       scanf("%d", &reply_menu1);
    
       if (reply_menu1 == 1)
       {
       printf("\n You have chosen for the METEOROLOGICAL program\n");
       meteorological_module();
       }
    
       if (reply_menu1 == 2)
       {
       printf("\n You have chosen for the MISCELLANEOUS program\n");
       miscelleneous_module();
       }
    
       if (reply_menu1 == 3)
       {
             printf ("\n Youv'e decided to QUIT, good-bye.");
       }
    
       else
       {
          printf ("\n Your choice is out of range. Please re-enter, make sure"
                  " your input is valid.");
       }
       getch();
       return 0;
    }

  2. #2
    tweedle beetle
    Join Date
    Oct 2004
    Posts
    7
    you can put everything inside a do...while()

    Code:
    int exit=0;
    do
    {
    
    All your other code here....
    
    
    
    printf(" %d. Enter three to exit from this menu"
                 " program\n", 3);
    
    if (reply_menu1 == 3)
       {
        exit=1;
       }
    
    } while (exit==0);
    
    the rest of the program here
    if reply_menu1 is greater than 3, then the loop repeats itself, and you see the menu again..

    hope this helps
    Last edited by m23oose; 10-20-2004 at 11:03 PM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Thanks for your reply, but it's no exactly what I'm after.

    3 or a higher number should exit from the program immediately, I can do that. However, I want the menu to be displayed again ONLY when the user enters an invalid value, such as a value less than 1 or a letter. from any sub-menu's I want to give the user the choice of returning back to the main menu, so not have the main menu displayed in front of them again when theyv'e inputted something in one of the sub-menus.
    Hope you understand, any ideas appreciated.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Anyone got any ideas on this?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Guti14
    Anyone got any ideas on this?
    Yeah, here's an idea! How about listening to the first reply? Don't tell me you can't figure it out from that.
    Code:
    while something
        while input != valid
            prompt for correct input
            get new input
        do something with the input
    They say what seperates man from beast is his thinking ability. I'm not so sure in your case.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    just a small tip ... put your menu in a seperate custom function like 'void menu()' to make your code easier to read.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  2. Systray menu messages
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2005, 06:25 AM
  3. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21