首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

fgets()与输入缓冲区的有关问题

2012-07-30 
fgets()与输入缓冲区的问题char infomsg[][50] {Intput account no.,Intput account name.,Intput

fgets()与输入缓冲区的问题
char infomsg[][50] = {
  "Intput account no.",
  "Intput account name.",
  "Intput sex[0:male, 1:fmale].",
  "Intput transcation amount.",
  "Intput transcation type."
  };
  while(条件)
  {
  setbuf(stdin, NULL);
  fprintf(stdout, "%s\n", infomsg[state - 1]);

  if (fgets(buffer ,sizeof(buffer), stdin) == NULL)
  {
  return 0;
  }
}

我执行时:第一次循环正常,第二次循环出问题了,直接跳过了account no的输入,就像fgets()没有执行,求高手解
Intput account no.
123123123
Intput account name.
asfsdaf
Intput sex[0:male, 1:fmale].
1
Intput transcation amount.
123
Intput transcation type.
sdf
A new record?[y/n]
y
Intput account no.
Intput account name.
asfsdaf

[解决办法]
单步调试,看看跳过了第二次输入的时候,那个fgets是否获得了一个回车键??
[解决办法]

探讨

加一句:fflush(stdin);试试吧。

热点排行