Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Menu/C/menu.c
Normal file
44
Task/Menu/C/menu.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *menu_select(const char *const *items, const char *prompt);
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *items[] = {"fee fie", "huff and puff", "mirror mirror", "tick tock", NULL};
|
||||
const char *prompt = "Which is from the three pigs?";
|
||||
|
||||
printf("You chose %s.\n", menu_select(items, prompt));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
const char *
|
||||
menu_select(const char *const *items, const char *prompt)
|
||||
{
|
||||
char buf[BUFSIZ];
|
||||
int i;
|
||||
int choice;
|
||||
int choice_max;
|
||||
|
||||
if (items == NULL)
|
||||
return NULL;
|
||||
|
||||
do {
|
||||
for (i = 0; items[i] != NULL; i++) {
|
||||
printf("%d) %s\n", i + 1, items[i]);
|
||||
}
|
||||
choice_max = i;
|
||||
if (prompt != NULL)
|
||||
printf("%s ", prompt);
|
||||
else
|
||||
printf("Choice? ");
|
||||
if (fgets(buf, sizeof(buf), stdin) != NULL) {
|
||||
choice = atoi(buf);
|
||||
}
|
||||
} while (1 > choice || choice > choice_max);
|
||||
|
||||
return items[choice - 1];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue