读书人

在第二回循环中fgets跳过

发布时间: 2012-08-07 14:54:48 作者: rapoo

在第二次循环中fgets跳过
linux环境下

char infomsg[][50] = {
"Intput account no.",
"Intput account name.",
"Intput sex[0:male, 1:fmale].",
"Intput transcation amount.",
"Intput transcation type."
};
while(state<=5)
{
setbuf(stdin, NULL);
fprintf(stdout, "%s\n", infomsg[state - 1]);

if (fgets(buffer ,sizeof(buffer), stdin) == NULL)
{
return 0;
}
swtich(state){
case 1:
if(条件){
...
state++;
}
break;
}
case 2:
if(条件){
...
state++;
}
break;
}
case 3:
if(条件){
...
state++;
}
break;
}
case 4:
if(条件){
...
state++;
}
break;
}
case 5:
if(条件){
...
state++;
}
break;
}
}

我在第一轮循环时,我都可以输入,第二次在循环时,直接跳过了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是否获得了一个回车键??
[解决办法]

探讨

引用:

引用:

引用:

十有八九是“回车、换行”之类的东东造成的

是啊,我在if (fgets(buffer ,sizeof(buffer), stdin) == NULL)这句之前加上了getchar()就行了,但是我不想这样做


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

在linux下 fflush……

读书人网 >C语言

热点推荐