Eclipse / MiniGW 中的 scanf 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1897227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with scanf in Eclipse / MiniGW
提问by Tyler Brock
I'm trying to run the following code in eclipse but the console remains blank until i stop the program at which point the output "Enter next value (<=0 to quit)2130567168 minutes is 35509452 hours, 48 minutes." is repeated over and over.
我正在尝试在 Eclipse 中运行以下代码,但控制台保持空白,直到我停止程序,此时输出“输入下一个值(<=0 退出)2130567168 分钟是 35509452 小时 48 分钟”。一遍又一遍地重复。
It seems that scanf is putting some default value in for some reason... can't figure out why. I'm not seeing anything before the program is stopped so i thought it might have to do with printf not being flushed, but I made sure to use \n to force a flush.
似乎 scanf 出于某种原因将一些默认值放入了......不知道为什么。在程序停止之前我什么也没看到,所以我认为这可能与 printf 没有被刷新有关,但我确保使用 \n 强制刷新。
Any ideas?
有任何想法吗?
#include <stdio.h>
const int MIN_PER_HOUR = 60; // minutes per hour
int main(void)
{
int hour, min, left;
printf("Convert minutes to hours and minutes!\n");
printf("Enter the number of minutes (<=0 to Quit):\n");
scanf("%d", &min); // read number of minutes
while(min > 0){
hour = min / MIN_PER_HOUR; // truncated number of hours
left = min % MIN_PER_HOUR; // number of minutes left over
printf("%d minutes is %d hours, %d minutes.\n", min, hour, left);
printf("Enter next value (<=0 to quit)");
scanf("%d", &min);
}
printf("Done!\n");
return 0;
}
采纳答案by unwind
Eclipse's terminal emulator might be different and do more buffering. Try calling fflush(stdout);
between the printout and the call to scanf()
.
Eclipse 的终端模拟器可能会有所不同,并进行更多缓冲。尝试fflush(stdout);
在打印输出和对 的调用之间调用scanf()
。
回答by Tyler Brock
I moved the code into visual C++ and it works as expected. I guess my questions should have been: "Why is the terminal emulation in eclipse broken?"
我将代码移到 Visual C++ 中,它按预期工作。我想我的问题应该是:“为什么 eclipse 中的终端仿真坏了?”
回答by pmg
If there's an invalid character for representing integers in the input for scanf()
, the function will stop at that character, leaving it in the buffer, ready to be read againand again, and again ...
如果在 for 的输入中存在用于表示整数的无效字符scanf()
,则函数将在该字符处停止,将其留在缓冲区中,准备好一次又一次地读取……
Suppose you entered "14 mins" for the first scanf()
. The function reads "14" and stops at the space. Then it assigns 14 to the variable min
and the program continues.
假设您为第一个输入了“14 分钟” scanf()
。该函数读取“14”并在空格处停止。然后它将 14 分配给变量min
,程序继续。
Then, inside the loop, the program executes another scanf()
. This time the input buffer already has stuff -- namely " mins" with the space. scanf()
will read and ignore the space, find an 'm' which it can't convert to an integer and stop without assigning a new value to the variable min
and returning a value of 0
. And again ... and again ... and again.
然后,在循环内,程序执行另一个scanf()
. 这一次输入缓冲区已经有了东西——即带有空间的“分钟”。scanf()
将读取并忽略空格,找到一个不能转换为整数的 'm' 并停止而不为变量分配新值min
并返回0
. 一次又一次……一次又一次……一次又一次。
So, what you have to do is to clear the input buffer after every scanf()
.
因此,您需要做的是在每个scanf()
.
How much of it do you clear?
Probably as far as the ENTER -- which is about the same fgets()
does :)
For example:
你清除了多少?
可能就 ENTER 而言 - 大致相同fgets()
:)
例如:
int ch;
scanf("%d", &min);
do { ch = getchar(); } while (ch != '\n'); /* empty buffer */