Anonymous user
Menu: Difference between revisions
→{{header|C}}: Fixed behaviour for non-integer input.
(→{{header|C++}}: corrected; simplified) |
(→{{header|C}}: Fixed behaviour for non-integer input.) |
||
Line 64:
#include <string.h>
int
▲const char *menu_select(const char **items, const char *prompt)
{
char *items[] = {"fee fie", "huff and puff", "mirror mirror", "tick tock", NULL};▼
char *prompt = "Which is from the three pigs?";
int choice;▼
if (items==NULL) return NULL;▼
for(i=0; items[i] != NULL; i++) {▼
printf("%d) %s\n", i+1, items[i]);▼
if ( prompt != NULL )▼
printf("%s ", prompt);▼
else▼
printf("Choice? ");▼
return items[choice-1];▼
▲ "fee fie", "huff and puff", "mirror mirror", "tick tock", NULL
▲};
char *
▲int main()
menu_select(char **items, char *prompt)
{
char buf[BUFSIZ];
▲ printf("You chose: %s\n", menu_select(menu, "Which is from the three pigs?"));
int i;
▲ return EXIT_SUCCESS;
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>
|