Linux C Programing - Terminal(1)
2016-03-18 14:04
357 查看
#include <stdio.h> //getchar() putchar() printf() gets() puts() sprintf() #include <stdlib.h> //exit() rand () srand() system() free() malloc() //int getchoice (char *greet , char *choices[]); int getchoice (char *greet , char **choices); int main() { char *menu[] = { "a - add new record", "d - delete record", "s - show record num ", "q - quit", NULL, }; int record_num = 0; int choice = 0; do { choice = getchoice("Please select an action",menu); printf("Your choice is %c \n",choice); switch (choice) { case 'a': { printf("Add record over\n"); record_num++; break; } case 'd': { printf("Remove a record over\n"); record_num--; break; } case 's': { printf("The record num is %d \n",record_num); break; } default: break; } } while(choice!='q'); exit(0); } int getchoice (char *greet , char **choices) { char **option; int chosen=0; // control choice is the member of menu list int selected; do { printf("Choice: %s \n" ,greet); option=choices; while (*option!=NULL) { printf("%s\n",*option); option++; } do { selected=getchar(); }while (selected=='\n'); // solve the enter char + '\n' problem,because will cause //next input enter the '\n' directly option =choices; while(*option) { if(selected == *option[0]) { chosen=1; break; } option++; } if (!chosen) { printf("Incorrect choice,try again\n"); } } while(!chosen); return selected; }
相关文章推荐
- Linux下autoconf
- Linux串口编程(中断方式和select方式)
- 《鸟哥的Linux私房菜:基础学习篇》读书笔记之第一部分
- 存储的大小端问题
- 解决linux与windows时间相差8小时
- jetty和jboss和linux的关联
- linq和samba和linux的关联
- mangodb和linux和linq的关联
- linux和linux和html的关联
- linux和linq和udp的关联
- linux下可变参数及宏定义封装函数
- Golang Linux下面调用nohup启动程序无法返回问题,解决方法.
- 【linux】rm[1]学习
- 监控 Linux 性能的命令与工具
- Linux常用命令
- linux下常用命令
- centos7.2之静态IP设置
- Linux常用压缩和解压命令
- CentOS之许可
- LINUX 内核代码 errno 错误代码提示 /include/asm/errno.h