Thread: Creating a menu system

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Creating a menu system

    How would I do this:

    Menu option 1:
    Menu option 2:
    Menu option 3:
    Menu option 4: Exits menu system

    Then the user selects one of the options by entering a number 1 to 3
    and the menu system needs to return the user to the menu . The only way I see is to use gotos , but as there is almost never a reason yo use gotos , I know there is a better way .

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Something like:
    Code:
    int ch;
    ch = getchar();
    
    switch(ch)
    {
        case '1':
            /* option 1 */
        break;
    
        case '2':
            /* option 2 */
        break;
    
        case '3':
            /* option 3 */
        break;
    
        case '4':
            /* exit */
        break;
    }

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Quote Originally Posted by zacs7 View Post
    Something like:
    Code:
    int ch;
    ch = getchar();
    
    switch(ch)
    {
        case '1':
            /* option 1 */
        break;
    
        case '2':
            /* option 2 */
        break;
    
        case '3':
            /* option 3 */
        break;
    
        case '4':
            /* exit */
        break;
    }
    So the break; quits out of the loop , and returns to the beginning of the loop ? I thought it just quits the program

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    break in this case exits the switch statement - instead of continuing to the next case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    What I mean is will the menu system loop in the above example if 1-3 is selected ?

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, it wasn't designed to be... however such a feature would be easy to implement...

    Hint: A while loop with a variable that acts like a flag,
    Code:
    while(looping)
    {
        ch = getchar();
    Bare in bind that you'll probably have to flush the input buffer before you try and ask for a menu selection (otherwise it could break if they entered, 14 for example) see the FAQ.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Thank you zacs7

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    This is what I came up with in pseudo , I'm not sure if exit is correct to use to break out of the loop .

    Code:
    While Looping=1 // While Loop, Used to loop the menu system
    	{	Input Choice	// The Choice variable will be used to select from one of the following menu options
    		If Choice='?'
    		Then 
    		{
    			Output " This is the help menu 
            		Enter ? for help , q to quit at the main menu . 
             		This program will calculate the exposure value for the entered :
             		Aperture , ISO and shutter speed . Press <Enter key> to continue,"
    			Looping<--1
    			exit
    		}
    	Else if Choice=’1’
    	Then 
    		{
    			Output “Please enter your name”
    			Input Name
    			exit
    		}
    	Else if Choice='2'
    	Then
    		{
    			output name," Please enter the ISO value "
    			Input ISO
    			For (i=0;len(ISO_VALID);i++)
    				If ISO==ISO_VALID[i]);  // Do strcmp() in C
    				Then
    				{
    						Output "You have entered an allowed value"
    						Looping<--1
    						exit
    					}
    		}
    	Else
    		{
    			Output "You have entered an invalid ISO value"
    			Looping<--1
    			exit
    		}
    
    	Else if Choice='4'
    	Then
    		{
    			Output "Please enter an aperture value"
    			Input guess
    			Looping<--1
    			exit
    		}
    	Else if Choice='5'
    	Then
    		{
    			Looping<--0
    		}
    }
    
    DoWhile

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    If its pseudocode you only need if, then, else. u cant include the " { "s but its past the due date already...

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Your pesudo code more speaks about the If-else-if statment which is something more smilar to switch statment. In that case when you say exit it means two thing 1. You are exiting from the switch 2. your exiting from thr whole program.

    So i would sugguest to use break instead of exit, if your main intention was to use switch. Which basically means that your exiting from your while loop.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating dynamic menu items
    By abachler in forum Windows Programming
    Replies: 2
    Last Post: 10-09-2008, 12:26 PM
  2. Creating a window through menu
    By Homunculus in forum Windows Programming
    Replies: 17
    Last Post: 02-20-2006, 06:56 PM
  3. creating a menu using C
    By whitey82 in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 12:43 PM
  4. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM
  5. Creating an Instant Messenging system...
    By Fool in forum C++ Programming
    Replies: 8
    Last Post: 08-13-2001, 06:41 PM