读书人

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

发布时间: 2012-07-30 16:19:05 作者: rapoo

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);试试吧。

读书人网 >C语言

热点推荐