Menu: Difference between revisions
Content added Content deleted
(→{{header|C++}}: corrected; simplified) |
(→{{header|C}}: Fixed behaviour for non-integer input.) |
||
Line 64: | Line 64: | ||
#include <string.h> |
#include <string.h> |
||
⚫ | |||
int |
|||
⚫ | |||
⚫ | |||
{ |
{ |
||
⚫ | |||
int i, nchoices; |
|||
char *prompt = "Which is from the three pigs?"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
while(1) { |
|||
⚫ | |||
⚫ | |||
} |
|||
nchoices = i; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
scanf("%d", &choice); |
|||
if ( (choice > 0) && (choice <= nchoices) ) break; |
|||
printf("Choose from 1 to %d\n\n", nchoices); |
|||
} |
|||
⚫ | |||
}</lang> |
|||
⚫ | |||
<lang c>const char *menu[] = { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
char * |
|||
⚫ | |||
menu_select(char **items, char *prompt) |
|||
{ |
{ |
||
char buf[BUFSIZ]; |
|||
⚫ | |||
int i; |
|||
⚫ | |||
⚫ | |||
int choice_max; |
|||
⚫ | |||
return NULL; |
|||
do { |
|||
⚫ | |||
⚫ | |||
} |
|||
choice_max = i; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if (fgets(buf, sizeof(buf), stdin) != NULL) { |
|||
choice = atoi(buf); |
|||
} |
|||
} while (1 > choice || choice > choice_max); |
|||
⚫ | |||
}</lang> |
}</lang> |
||