Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to make a menu

Reply
Thread Tools

How to make a menu

 
 
dapernia
Guest
Posts: n/a
 
      08-18-2003

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
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      08-18-2003
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[i].option_number,
main_menu[i].text);
}
/* enter the selection ... */
/* now process the selection */
for (i = 0; i < NUM_MAIN_OPTIONS; ++i)
{
if (selection == main_menu[i].option_number)
{
if (main_menu[i].process_function)
main_menu[i].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

 
Reply With Quote
 
 
 
 
ArWeGod
Guest
Posts: n/a
 
      08-18-2003
"dapernia" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-20-2003
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
only wanna show Sub Menu of root menu mike ASP .Net 2 11-30-2005 10:50 PM
Horizontal Menu and Sub menu in asp.net 2.0 itzikkl ASP .Net 0 03-27-2005 12:50 PM
Menu and sub-menu in asp.net as windows menus made with vb, delphi, etc..? Vilmar ASP .Net 0 05-27-2004 05:52 PM
Jump Menu (DropDown Menu) in ASP.net. Can someone help me out? Miguel Dias Moura ASP .Net 3 04-03-2004 08:17 AM



Advertisments