Jump to content

Menu: Difference between revisions

797 bytes added ,  14 years ago
(→‎{{header|Common Lisp}}: force-output suffices, move shorter if case first, indentation, use ~& at beginning of output)
Line 59:
sel$ = ret$
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}}==
<lang lisp>(defun select (prompt choices)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.