-
Creating a menu system
How would I do this:
Menu option 1:
Menu option 2:
Menu option 3:
Menu option 4: Exits menu system
Then the user selects one of the options by entering a number 1 to 3
and the menu system needs to return the user to the menu . The only way I see is to use gotos , but as there is almost never a reason yo use gotos , I know there is a better way .
-
Something like:
Code:
int ch;
ch = getchar();
switch(ch)
{
case '1':
/* option 1 */
break;
case '2':
/* option 2 */
break;
case '3':
/* option 3 */
break;
case '4':
/* exit */
break;
}
-
Quote:
Originally Posted by
zacs7
Something like:
Code:
int ch;
ch = getchar();
switch(ch)
{
case '1':
/* option 1 */
break;
case '2':
/* option 2 */
break;
case '3':
/* option 3 */
break;
case '4':
/* exit */
break;
}
So the break; quits out of the loop , and returns to the beginning of the loop ? I thought it just quits the program
-
break in this case exits the switch statement - instead of continuing to the next case
-
What I mean is will the menu system loop in the above example if 1-3 is selected ?
-
No, it wasn't designed to be... however such a feature would be easy to implement...
Hint: A while loop with a variable that acts like a flag,
Code:
while(looping)
{
ch = getchar();
Bare in bind that you'll probably have to flush the input buffer before you try and ask for a menu selection (otherwise it could break if they entered, 14 for example) see the FAQ.
-
-
This is what I came up with in pseudo , I'm not sure if exit is correct to use to break out of the loop .
Code:
While Looping=1 // While Loop, Used to loop the menu system
{ Input Choice // The Choice variable will be used to select from one of the following menu options
If Choice='?'
Then
{
Output " This is the help menu
Enter ? for help , q to quit at the main menu .
This program will calculate the exposure value for the entered :
Aperture , ISO and shutter speed . Press <Enter key> to continue,"
Looping<--1
exit
}
Else if Choice=’1’
Then
{
Output “Please enter your name”
Input Name
exit
}
Else if Choice='2'
Then
{
output name," Please enter the ISO value "
Input ISO
For (i=0;len(ISO_VALID);i++)
If ISO==ISO_VALID[i]); // Do strcmp() in C
Then
{
Output "You have entered an allowed value"
Looping<--1
exit
}
}
Else
{
Output "You have entered an invalid ISO value"
Looping<--1
exit
}
Else if Choice='4'
Then
{
Output "Please enter an aperture value"
Input guess
Looping<--1
exit
}
Else if Choice='5'
Then
{
Looping<--0
}
}
DoWhile
-
If its pseudocode you only need if, then, else. u cant include the " { "s but its past the due date already... :)
-
Your pesudo code more speaks about the If-else-if statment which is something more smilar to switch statment. In that case when you say exit it means two thing 1. You are exiting from the switch 2. your exiting from thr whole program.
So i would sugguest to use break instead of exit, if your main intention was to use switch. Which basically means that your exiting from your while loop.
ssharish