Menu: Difference between revisions
Content added Content deleted
(→{{header|Common Lisp}}: force-output suffices, move shorter if case first, indentation, use ~& at beginning of output) |
(→{{header|Common Lisp}}: ++ C) |
||
Line 59: | Line 59: | ||
sel$ = ret$ |
sel$ = ret$ |
||
end function</lang> |
end function</lang> |
||
=={{header|C}}== |
|||
<lang c>#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
char *menu_select(char **items, char *prompt) |
|||
{ |
|||
int i, nchoices; |
|||
int choice; |
|||
if (items==NULL) return NULL; |
|||
while(1) { |
|||
for(i=0; items[i] != NULL; i++) { |
|||
printf("%d) %s\n", i+1, items[i]); |
|||
} |
|||
nchoices = i; |
|||
if ( prompt != NULL ) |
|||
printf("%s ", prompt); |
|||
else |
|||
printf("Choice? "); |
|||
scanf("%d", &choice); |
|||
if ( (choice > 0) && (choice <= nchoices) ) break; |
|||
printf("Choose from 1 to %d\n\n", nchoices); |
|||
} |
|||
return items[choice-1]; |
|||
}</lang> |
|||
<lang c>char *menu[] = { |
|||
"fee fie", "huff and puff", "mirror mirror", "tick tock", NULL |
|||
}; |
|||
int main() |
|||
{ |
|||
printf("You chose: %s\n", menu_select(menu, "Which is from the three pigs?")); |
|||
return EXIT_SUCCESS; |
|||
}</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lang lisp>(defun select (prompt choices) |
<lang lisp>(defun select (prompt choices) |