as far as i understand:
#include <stdio.h>
#include <string.h>
void pause(void);
void print_result(char **menu, int ret);
void clrscr(void);
#define MENU_INPUT(a) menu_input(a,sizeof(a)/sizeof(*a))
char * main_menu[]={
"Display First Menu",
"Display Sencond Menu"
};
char * first_menu[]={
"Format Drive C:",
"Partition First Drive",
"Scan Drive"
};
char * second_menu[]={
"Display an image",
"Play a sound"
};
int menu_input(char **menu,int items){
int i, j;
clrscr();
for(i = 1; i<=items;i++)
printf(" %d - %s\n",i, menu[i-1]);
printf(" 0 - to cancel\n ");
do{
scanf("%d", &i);
}while( i < 0 || i > items);
return i;
}
int main(void){
int ret;
for( ; ; ){
switch( MENU_INPUT( main_menu ) ){
case 0:
return 0;
case 1:
ret = MENU_INPUT( first_menu );
print_result( first_menu, ret );
break;
case 2:
ret= MENU_INPUT(second_menu);
print_result( second_menu, ret );
}
}
return 0;
}
// these are some helpper functions
void pause(void){
char c;
printf("press enter to continue\n");
scanf(" %c", &c);
}
void print_result(char **menu, int ret){
if( ret ){
printf("You pressed %d (%s)\n", ret, first_menu[ ret - 1 ] );
pause();
}
}
// clear screen
void clrscr(void){
#ifdef WIN32
char *cmd = "cls";
#else
char *cmd = "tput reset";
#endif
system(cmd);
}
1
solved Need some help figuring out how to do 2 things in my C program [closed]