我用的是VC6.0,写些小程序有个习惯,就是用while(flag){do sth.}来循环表示菜单,若需要退出则在while里将flag置0。 |
|
15分 |
#include <stdio.h> #define TRUE 1 #define FALSE 0 char DisplayMenu() { int order; printf("**The order list is as follows:\n"); printf("**10 - do what\n"); printf("**11 - do what\n"); printf("**20 - do what\n"); printf("**21 - do what\n"); printf("**30 - do what\n"); printf("**31 - do what\n"); printf("**00 - Exit\n"); while (1) { printf("Please input your order:");fflush(stdout); rewind(stdin); if (1==scanf("%d", &order)) { if (order==0 || order==10 || order==11 || order==20 || order==21 || order==30 || order==31) { break; } } printf("Illegal order!\n"); } return order; } char ExecuteOrder(char order) { switch (order) { case 10: { do what return TRUE; } case 11: { do what return TRUE; } case 20: { do what return TRUE; } case 21: { do what return TRUE; } case 30: { do what return TRUE; } case 31: { do what return TRUE; } case 0: { return FALSE; } } } int main(void) { char instruction = 0; char flag = TRUE; while(flag) { instruction = DisplayMenu(); flag = ExecuteOrder(instruction); } return 0; } |
5分 |
你需要
fflush(stdout); |
哦,原来如此,我对标准输入输出流了解还不够深,终于找到原因啦,太谢谢了。
|