menu2.c
来自「《Beginning linux programming》一书相应的源代码」· C语言 代码 · 共 58 行
C
58 行
#include <unistd.h>#include <stdio.h>char *menu[] = { "a - add new record", "d - delete record", "q - quit", NULL,};int getchoice(char *greet, char *choices[]);int main(){ int choice = 0; if(!isatty(fileno(stdout))) { fprintf(stderr,"You are not a terminal!\n"); exit(1); } do { choice = getchoice("Please select an action", menu); printf("You have chosen: %c\n", choice); } while (choice != 'q'); exit(0);}int getchoice(char *greet, char *choices[]){ int chosen = 0; int selected; char **option; do { printf("Choice: %s\n",greet); option = choices; while(*option) { printf("%s\n",*option); option++; } selected = getchar(); option = choices; while(*option) { if(selected == *option[0]) { chosen = 1; break; } option++; } if(!chosen) { printf("Incorrect choice, select again\n"); } } while(!chosen); return selected;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?