How to make a menu

Discussion in 'C Programming' started by dapernia, Aug 18, 2003.

  1. dapernia

    dapernia Guest

    Hi friends, I want to make menu in my C program

    For example:

    Main Menu: (1) Enter Data, (2) Control Variables (3) Exit

    I want to make this menu to go through the options just pressing numbers
    1,2 and 3.

    Thanks Daniel


    --
    Posted via http://dbforums.com
     
    dapernia, Aug 18, 2003
    #1
    1. Advertising

  2. dapernia wrote:

    > Hi friends, I want to make menu in my C program
    >
    > For example:
    >
    > Main Menu: (1) Enter Data, (2) Control Variables (3) Exit
    >
    > I want to make this menu to go through the options just pressing numbers
    > 1,2 and 3.
    >
    > Thanks Daniel
    >
    >
    > --
    > Posted via http://dbforums.com


    One method for menus is to use tables.
    typedef void (*Ptr_To_Function)(void);
    struct Menu_Record
    {
    unsigned int option_number;
    const char * text;
    Ptr_To_Function process_function;
    };

    void Enter_Data(void);
    void Control_Variables(void);

    struct Menu_Record main_menu[] =
    {
    {1, "Enter Data", Enter_Data},
    {2, "Control Variables {before the get out of control}",
    Control_Variables},
    {3, "Exit", NULL}
    };
    const unsigned int NUM_MAIN_OPTIONS =
    sizeof (main_menu) / sizeof(main_menu[0]);

    int main(void)
    {
    unsigned int i;
    printf("Main Menu:\n");
    for (i = 0; i < NUM_MAIN_OPTIONS; ++i)
    {
    printf("\t(%d) %s\n",
    main_menu.option_number,
    main_menu.text);
    }
    /* enter the selection ... */
    /* now process the selection */
    for (i = 0; i < NUM_MAIN_OPTIONS; ++i)
    {
    if (selection == main_menu.option_number)
    {
    if (main_menu.process_function)
    main_menu.process_function();
    break;
    }
    }
    return EXIT_SUCCESS;
    }


    This menu handling method allows you to have many menus
    but only require one driver to process the tables. The
    number of options can shrink or expand without having
    to change the execution code (driver). This method can
    be expanded to handle sub-menus too.

    It is a nice feeling to spend less than 10 minutes to
    update a menu by adding in a new row, rather than having
    to locate a switch (or if-else ladder), modify it and
    retest (which could take hours or days).

    --
    Thomas Matthews
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
     
    Thomas Matthews, Aug 18, 2003
    #2
    1. Advertising

  3. dapernia

    ArWeGod Guest

    "dapernia" <> wrote in message
    news:...
    >
    > Hi friends, I want to make menu in my C program
    >
    > For example:
    >
    > Main Menu: (1) Enter Data, (2) Control Variables (3) Exit
    >
    > I want to make this menu to go through the options just pressing numbers
    > 1,2 and 3.
    >
    > Thanks Daniel
    >
    >
    > --
    > Posted via http://dbforums.com



    I just answered that under a question asking what conio.h was. If you use
    MSDOS, the standard compilers (Borland, Microsoft,etc) add functions to
    query if a key is waiting, read the keyboard, push back a key to the
    keyboard buffer, directly write to console (much faster than printf()), etc.

    --
    ArWeGod@sbc_global.net
    Remove underscore to email.
     
    ArWeGod, Aug 19, 2003
    #3
  4. dapernia

    CBFalconer Guest

    dapernia wrote:
    >
    > For example:
    >
    > Main Menu: (1) Enter Data, (2) Control Variables (3) Exit
    >
    > I want to make this menu to go through the options just pressing
    > numbers 1,2 and 3.


    Without using ENTER or CR or whatever your terminal provides, not
    possible under pure ISO C. However, just to add to the confusion,
    you can use a terminal with programmable function keys, and
    program 3 of them to generate the sequences "0\n", "1\n", "2\n".
    This gives the operator the effect desired.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura
    Replies:
    3
    Views:
    4,342
    Bruno Sirianni
    Apr 3, 2004
  2. Vilmar
    Replies:
    0
    Views:
    484
    Vilmar
    May 27, 2004
  3. itzikkl
    Replies:
    0
    Views:
    5,802
    itzikkl
    Mar 27, 2005
  4. mike
    Replies:
    2
    Views:
    607
  5. Bryan
    Replies:
    1
    Views:
    795
    Steven Cheng[MSFT]
    Mar 1, 2006
Loading...

Share This Page