Thread: Need help on making a menu system for my program

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Need help on making a menu system for my program

    I have been given the task to create a system where I have to create a system for conversions which are in four sections weight , measurement , distance and time so for example this would be main page.

    Main menu

    -----------------------------------------------------------------
    Please type the name of the category you would view _

    1.Weight
    2. Measurement
    3.Distance
    4.Time
    -----------------------------------------------------------------
    Then this would go into sub menu. Example if I typed Distance In
    -----------------------------------------------------------------
    Conversions for Distance

    Please type on the conversion you would like to use
    1.Mile's to kilometre
    2. Kilomere to mile's
    3.Main me
    -----------------------------------------------------------------
    Then you would do the conversion. But I know how I wanted it to layout but I have no idea to start and this menu system
    If any one can give some code that will help or a starting point it would be very much appreciated

  2. #2
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    You could use fgets and sscanf to get an input from the user and then do if clauses or switches for the options (As in If (input==1) printf("The menu"), etc...)

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    it would be sth like this...
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    int menu(void);
    int distance(void);
     int sw;
    int main()
    {
        menu();
    }
    
    
    int menu()
    {
        system("cls");
    
    
     printf(" 1.Weight\n");
     printf(" 2.Measurement\n");
     printf(" 3.Distance\n");
     printf(" 4.Time \n ");
      sw=getch();
      sw=sw-'0';
       switch(sw)
       {
         case 1:
         //code....
         break;
    
    
        case 2:
        //...
        break;
    
    
        case 3:
        distance();
        break;
    
    
        case 4:
        //...
        break;
    
    
        default:
        menu();
        break;
       }
    }
    int distance()
    {
        system("cls");
        printf(" 1.Mile's to kilometre\n");
        printf(" 2.Kilomere to mile's\n");
        printf(" 3.Main me\n");
      sw=getch();
      sw=sw-'0';
       switch(sw)
       {
         case 1:
         //code...
         break;
    
    
        case 2:
        //code...
        break;
    
    
        case 3:
        main();
        break;
    
    
        default:
        distance();
        break;
       }
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    king_zart's solution uses switch statements, which are a great way to handle menu choices, however there are some potential problems with other parts of his program.

    The first thing that jumps out is the use of global variables (sw in this case). There are almost always better solutions. Declare the variable in the appropriate function and pass it around as needed. Read this to learn why globals are bad: Global Variables Are Bad.

    Second, cls is a Windows specific command, so unless you're using Windows, this wont work. Even if you are, other people may not be, so they can't use it. Plus, many people find it annoying when the terminal keeps clearing itself.

    Third, getch() is non-standard function. You could use getchar(), but that comes with it's own set of problems, since if you hit the enter key, it will treat that as a menu choice. scanf("%d", &choice) is one option, though I might use fgets to read a whole line, and sscanf or strtol to parse out the numeric choice.

    Lastly, don't use recursion to repeat menus. This can be a problem when your program is used for a long time without ever quitting. What happens is that each function call gets placed on the stack, and they keep piling up because the function never gets a chance to return. Eventually this can cause you program to crash. Instead, I recommend a do loop:
    Code:
    do {
        valid_choice = 1;  // assume user input is valid
        print menu choices
        read input  // set to something invalid like -1, if reading fails
        switch (input) {
            case 1:
                get miles
                convert to km
                print result
                break;
           ...
           case 3:
               break;  // go back to main menu
           default:
               valid_choice = 0;  // bad input
               break;
        }
    } while (!valid_input  || input != 3);
    Note, depending on whether you read the menu choice as an int or a char, you may need to use case 1: or case '1': (note the single quotes).

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    Thank's for everyone's input it helped me alot.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I still prefer a menu function though, something like:

    Code:
    function menuChoice:
       display choices
       get a choice
       while choice is not valid:
          display error
          get a choice
       end while
       return choice
    It can be really easy to use.
    Code:
     while ((choice = menuChoice()) != QUIT_CHOICE) ...

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Hi I am doing trying to do a conversion code also but cant get the conversions to work properly
    I have done a miles to kilometres conversion at the bottom

    Code:
    #include <stdio.h>#include<stdlib.h>
    
    
    int menu();
    int distance();
    int sw;
    int main()
    {
        menu();
        float value;
        value=distance();
        printf("%f", value);
        return 0;
    
    
    }
    
    
    
    
    int menu()
    {
    system("cls");
    
    
    printf(" Please selcet a category\n");
    printf(" 1.Weight\n");
    printf(" 2.Measurement\n");
    printf(" 3.Distance\n");
    printf(" 4.Tempereture \n ");
    sw=getch();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    //code....
    break;
    
    
    
    
    case 2:
    //...
    break;
    
    
    
    
    case 3:
    distance();
    break;
    
    
    
    
    case 4:
    //...
    break;
    
    
    
    
    default:
    menu();
    break;
    }
    }
    int distance()
    {
    system("cls");
    printf(" 1.Miles to kilometres\n");
    printf(" 2.Kilomeres to miles\n");
    printf(" 3.Return to the Main menu\n");
    sw=getch();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    MtoK();
    break;
    
    
    
    
    case 2:
    //code...
    break;
    
    
    
    
    case 3:
    main();
    break;
    
    
    
    
    default:
    distance();
    break;
    }
    }
    int MtoK()
    {
    float miles, kilometres;
    system("cls");
    printf("\t\tMiles to Kilometres Conversion\n");
    printf("\t\t------------------------------\n");
    printf("\t\tEnter number of Miles: ");
    kilometres=miles/0.621371192;
    
    
    printf("\t\t%2.2f mile(s) == %2.2f kilometre(s)",miles, kilometres);
    printf("\n\n\tPress enter to return to the Main Menu");
    getchar();
              return kilometres;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    @binbag - you've got your own thread here -> Help me
    No need to hijack someone else's thread, even if you seem to be in the same class, or have the same kind of question.

    Though you should really read post #4 to understand why the example you copy/pasted is not the best approach.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Displays menu twice after making a selection
    By jacobj86 in forum C Programming
    Replies: 4
    Last Post: 03-08-2012, 02:03 PM
  2. [C++] Need Help Making Menu Program
    By Bartvo in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2010, 12:14 AM
  3. Creating a menu system
    By ICool in forum C Programming
    Replies: 9
    Last Post: 09-17-2007, 12:18 PM
  4. System Menu
    By Necrofear in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2006, 06:14 AM
  5. Making a new window from a menu option
    By LD_Dreamer in forum Windows Programming
    Replies: 2
    Last Post: 12-28-2002, 09:08 PM