Here is an example of how to create a menu system. Granted mine is a bit fancy but... well... its what I came up with:
CODE
#include <stdio.h>
//-------- Some Menu Prompts that we can use -----------
static char menuPrompts[][64] = {
"Enter the world of tomarrow",
"Sell your soul to Microsoft",
"Become a dot-com billionare",
"Invest poorly in internet fads",
"Write a more influential book than Knuth",
"End it all (exit program)"
};
//an enumeration listing all of the prompts...
enum prompts {
WORLD_OF_TOMARROW, //==0
SOUL_TO_MICROSOFT, //==1
DOTCOM_BILLIONARE, //==2
POOR_INTERNET_FAD, //==3
OUT_SELL_KNUTH, //==4
EXIT_PROGRAM, //==5
NUMBER_OF_MENU_ITEMS //This "counts" how many prompts we made
};
//Define how manu tries the user gets
#define MAX_USER_TRIES 3
//--- function prototypes ----
void displayMenu(char menuItems[][64], int num);
int get_users_input(int max);
int getLine(char *buffer, int length);
void scrollScr();
int main() {
int choice = 0;
//Generally menus are done like this...
do {
//First you display the menu
displayMenu(menuPrompts, NUMBER_OF_MENU_ITEMS);
//Then get the user's choice
choice = get_users_input(NUMBER_OF_MENU_ITEMS);
//Use a control structure like a switch or if-statement to control
// the flow of the program
switch (choice) {
case WORLD_OF_TOMARROW:
{
//here is where we would do something related to user's choice...
printf("\nUser wants to: %s\n\n",menuPrompts[WORLD_OF_TOMARROW]);
} break;
case SOUL_TO_MICROSOFT:
{
printf("\nUser wants to: %s\n\n",menuPrompts[SOUL_TO_MICROSOFT]);
} break;
case DOTCOM_BILLIONARE:
{
printf("\nUser wants to: %s\n\n",menuPrompts[DOTCOM_BILLIONARE]);
} break;
case POOR_INTERNET_FAD:
{
printf("\nUser wants to: %s\n\n",menuPrompts[POOR_INTERNET_FAD]);
} break;
case OUT_SELL_KNUTH:
{
printf("\nUser wants to: %s\n\n",menuPrompts[OUT_SELL_KNUTH]);
} break;
case NUMBER_OF_MENU_ITEMS:
{
scrollScr();
} break;
}
//The whole thing is put into a loop and we look for the exit condition(s).
} while (choice!=EXIT_PROGRAM);
return 0;
}
//This simply prints out a numbered list of strings
void displayMenu(char menuItems[][64], int num) {
int i;
for (i = 0; i < num; i++) {
printf("% 2d. %s\n", i+1, menuItems[i]);
}
}
//This routine gets the users input: if the user does not enter a number > 0 then
// the user will be prompted again up to MAX_USER_TRIES times.
int get_users_input(int max) {
int choice = 0;
int tries = 0;
char inBuffer[10];
while (!choice && tries < MAX_USER_TRIES) {
printf("\nEnter a choice (1 - %d): ", max);
getLine(inBuffer, 10); //read in the user's input
sscanf(inBuffer, "%d", &choice); //see if it is a valid number
tries++;
}
return (tries < MAX_USER_TRIES) ? choice - 1 : max;
}
//An input routine: returns the number of characters read in.
// the routine does have a flaw though... it adds the 0x0A from the
// CRLF on windows systems... so if the user only presses 1 key then enter
// the routine returns 2.
int getLine(char *buffer, int length) {
int i = 0;
while(i < length && (buffer[i++] = getchar()) != '\n');
buffer[i] = 0x00;
return i-1;
}
//Since all the clear screen methods are platform dependant, this will
// scroll the screen a bunch so clear away old data
void scrollScr() {
int i;
for (i=0; i<25; i++) puts("\n");
}
now you don't have to be so fancy, the basic idea is to print out a menu, get the user's choice, respond to the choice...