所以。用C编写应用程序,unix,我有函数,它要求用户输入,直到输入有效。 让直行:循环运行超过预期C
int inputX()
{
int result;
char input;
while(1)
{
printf("Input x (a, b, c, d, e, f, g, h) :");
scanf("%c", &input);
result = validChar(input);
if (result >= 0)
{
return result;
}
else
{
printf("Invlid input. Lets try again.\n");
}
}
}
validChar()检查,如果输入的是 - 小时。 如果我输入 - 一切都很好。 如果我输入例如1,输出是
Input x (a, b, c, d, e, f, g, h) :1
Invlid input. Lets try again.
Input x (a, b, c, d, e, f, g, h) :Invlid input. Lets try again.
Input x (a, b, c, d, e, f, g, h) :
我甚至试图睡眠(),但没有工作.. C是棘手:|
为什么这个问题标记为'C++'? – 2012-03-05 19:54:26
我怀疑有些数据留在输入缓冲区中。你在第二次迭代中检查了“输入”的内容吗?它会告诉你很多...... – 2012-03-05 19:55:56
[C循环可能重复两次打印字符串? (使用scanf(“%c”))](http://stackoverflow.com/questions/9421926/c-loop-prints-string-twice-using-scanfc) – 2012-03-05 19:57:05