几个小知识点:

字符串数组作为参数。

fileno(stdout)的使用。

标准输出是否被重定向了   isatty( fileno(stdout) );

回车符的过滤方法。

 

#include <stdio.h>
#include <stdlib.h>

char *menu[] = {
  "a add",
  "b back",
  "c cancel",
  "q quit",
  NULL
};

 

//函数参数类型

static int getChoice(char* str, char* menu[])
{
  int choose;
  int c;
  char** p = NULL;
  int select = 0;

  do{
      select = 0;
      printf("%s\n", str);

      p = menu;
      while(*p)
      {
        printf("%s\n", *p);
        p++;
      }

      //过滤掉输入的回车符

      do{
          choose = getchar();
       }while(choose == '\n');


      p = menu;
      while(*p)
      {
        if(*p[0] == choose)
        {
          select = 1;
          break;
        }

        p++;
      }

      if(!select)
      {
        printf("incorrect input\n");
      }


  }while(!select);


  return choose;
}

int main()
{

  int choose;

  do{

      choose = getChoice("Please input command", menu);
      printf("you have choose %c\n", choose);
  }while(choose != 'q');

  exit(0);
}

 

posted on 2016-03-25 15:32  邶风  阅读(274)  评论(0编辑  收藏  举报