Thread: Help with menu

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    Help with menu

    I am trying to make a menu which will continue until option 4 is pressed except it ends after each option. How do I make it go back to the menu after each option until option 4 is pressed?

    Code:
    int main(void)
    {
    printf(MENU);
    printf(Option 1);
    printf(Option 2);
    printf(Option 3);
    printf(Option 4);
    scanf("%d", &menu)
    
    switch(menu)
    {
    case 1: Option1();
    break;
    case 2: Option2();
    break;
    case 3: Option3();
    break;
    case 4: Option4();
    break;
    default: printf("Wrong Input");
    break;
    }
    }
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put it all in a loop. Oh, and you'll want quotes around the "Option 1" stuff.
    Code:
    do
    {
        ...all of this at least once...
    } while( condition == true );
    Or...
    Code:
    while( condition == true )
    {
        do all of this
    }
    Or...
    Code:
    for( initialize; condition == true; update )
    {
        do all of this
    }

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

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Man what is it with newbies and reading the %%^&* code posting guidelines? That indentation looks like poo eym. First your scanf call has no ; at the end so this could never compile. What is MENU? for that matter, what is menu?
    Anyhow, I fixed your printf()s, added your functions, fixed your scanf() and provided a while loop that lets you keep getting user inputs, when they press 4<enter> it exits...
    Code:
    #include <stdio.h>
    
    
    void Option1()
    {
       printf("Option 1\n");
       return;
    }
    
    void Option2()
    {
       printf("Option 2\n");
       return;
    }
    void Option3()
    {
       printf("Option 3\n");
       return;
    }
    void Option4()
    {
       printf("Option 4, quitting\n");
       return;
    }
    
    int main(void)
    {
       printf("MENU\n");
       printf("Option 1\n");
       printf("Option 2\n");
       printf("Option 3\n");
       printf("Option 4 to quit...\n");
       int menu = 0;
       bool bQuit = false;
       while(!bQuit)
       {
          scanf("%d", &menu);
    
          switch(menu)
          {
    	 case 1: Option1();
    	    break;
    	 case 2: Option2();
    	    break;
    	 case 3: Option3();
    	    break;
    	 case 4: 
    	 {
    	    Option4();
    	    bQuit = true;
    	    break;
    	 }
    	 default: printf("Wrong Input");
    	    break;
          }
       }
       printf("Exiting...\n");
       return 0;
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM