S
sugaray
Right now, the code I wrote below seems works fine, i know it's definitely
not the best solution, so if there are better solutions to
eschew from using goto statements to jump out of multiple loops ?
and any other suggestions or good ideas that are pertain to construct
a more powerful and common interactive menu system is also welcomed,
and if there's any primitive but better interactive system that is
written in C suitable for educational purpose you happen to know of,
let me know please, i need a good and relatively easy one to tamper
with in order to build my skill, thanx in advance for your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char prompt = '#';
int prompt_mode = 0;
static void
promptmode (void)
{
int i;
for (i = 0; i < prompt_mode; ++i)
printf ("%c", prompt);
printf (">");
}
static void
level1menu (void)
{
printf ("\n=========== LEVEL-1 MENU ============\n");
printf ("| 1. HELP MESSAGES |\n");
printf ("| 2. ENTER LEVEL-2 |\n");
printf ("| 3. DISPLAY THIS MENU |\n");
printf ("| 4. QUIT THE SYSTEM |\n");
printf ("=====================================\n\n");
}
static void
level2menu (void)
{
printf ("\n*********** LEVEL-2 MENU ************\n");
printf ("| 1. HELP MESSAGES |\n");
printf ("| 2. FUNCTIONALITIES |\n");
printf ("| 3. DISPLAY THIS MENU |\n");
printf ("| 4. BACK TO THE UPPER LEVEL |\n");
printf ("*************************************\n\n");
}
static int
getchoice (void)
{
char input[4];
fgets (input, sizeof input, stdin);
fflush (stdout);
input[strlen (input) - 1] = '\0';
return atoi (input);
}
int
main (void)
{
int choice;
level1:
level1menu ();
for (;
{
prompt_mode = 1;
fflush(stdin);
promptmode ();
choice = getchoice ();
if (choice < 1 || choice > 4)
continue;
switch (choice)
{
case 1:
printf ("help messages for level 1.\n");
break;
case 2:
prompt_mode = 2;
level2menu ();
for (;
{
fflush(stdin);
promptmode ();
choice = getchoice ();
if (choice < 1 || choice > 4)
continue;
switch (choice)
{
case 1:
printf ("display help messages for level 2.\n");
break;
case 2:
printf ("implement functionalities.\n");
break;
case 3:
level2menu ();
break;
case 4:
printf ("back to the upper level.\n");
goto level1;
default:
printf ("invalid option in level 2.\n");
}
}
break;
case 3:
level1menu ();
break;
case 4:
goto end;
default:
printf ("invalid option in level 1.\n");
}
}
end:
printf ("\nsee ya \n");
return 0;
}
not the best solution, so if there are better solutions to
eschew from using goto statements to jump out of multiple loops ?
and any other suggestions or good ideas that are pertain to construct
a more powerful and common interactive menu system is also welcomed,
and if there's any primitive but better interactive system that is
written in C suitable for educational purpose you happen to know of,
let me know please, i need a good and relatively easy one to tamper
with in order to build my skill, thanx in advance for your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char prompt = '#';
int prompt_mode = 0;
static void
promptmode (void)
{
int i;
for (i = 0; i < prompt_mode; ++i)
printf ("%c", prompt);
printf (">");
}
static void
level1menu (void)
{
printf ("\n=========== LEVEL-1 MENU ============\n");
printf ("| 1. HELP MESSAGES |\n");
printf ("| 2. ENTER LEVEL-2 |\n");
printf ("| 3. DISPLAY THIS MENU |\n");
printf ("| 4. QUIT THE SYSTEM |\n");
printf ("=====================================\n\n");
}
static void
level2menu (void)
{
printf ("\n*********** LEVEL-2 MENU ************\n");
printf ("| 1. HELP MESSAGES |\n");
printf ("| 2. FUNCTIONALITIES |\n");
printf ("| 3. DISPLAY THIS MENU |\n");
printf ("| 4. BACK TO THE UPPER LEVEL |\n");
printf ("*************************************\n\n");
}
static int
getchoice (void)
{
char input[4];
fgets (input, sizeof input, stdin);
fflush (stdout);
input[strlen (input) - 1] = '\0';
return atoi (input);
}
int
main (void)
{
int choice;
level1:
level1menu ();
for (;
{
prompt_mode = 1;
fflush(stdin);
promptmode ();
choice = getchoice ();
if (choice < 1 || choice > 4)
continue;
switch (choice)
{
case 1:
printf ("help messages for level 1.\n");
break;
case 2:
prompt_mode = 2;
level2menu ();
for (;
{
fflush(stdin);
promptmode ();
choice = getchoice ();
if (choice < 1 || choice > 4)
continue;
switch (choice)
{
case 1:
printf ("display help messages for level 2.\n");
break;
case 2:
printf ("implement functionalities.\n");
break;
case 3:
level2menu ();
break;
case 4:
printf ("back to the upper level.\n");
goto level1;
default:
printf ("invalid option in level 2.\n");
}
}
break;
case 3:
level1menu ();
break;
case 4:
goto end;
default:
printf ("invalid option in level 1.\n");
}
}
end:
printf ("\nsee ya \n");
return 0;
}