Eclipse / MiniGW中的scanf问题 [英] Problem with scanf in Eclipse / MiniGW
问题描述
似乎scanf由于某种原因放置了一些默认值...无法弄清楚为什么。在程序停止之前,我没有看到任何东西,所以我认为这可能与printf没有被刷新有关,但是我确保使用\来强制刷新。
任何想法?
#include< stdio.h>
const int MIN_PER_HOUR = 60;每小时//分钟
int main(void)
{
int小时,最小,左;
printf(转换分钟到小时和分钟!\\\
);
printf(输入分钟数(< = 0退出):\\\
);
scanf(%d,& min); //读取分钟数
while(min> 0){
hour = min / MIN_PER_HOUR; // truncated hours of hours
left = min%MIN_PER_HOUR; //剩下的分钟数
printf(%d分钟是%d小时,%d分钟。,分钟,小时,左);
printf(输入下一个值(< = 0以退出));
scanf(%d,& min);
}
printf(Done!\\\
);
return 0;
}
Eclipse的终端仿真器可能不同并做更多的缓冲。尝试在打印输出和调用 scanf()
之间调用 fflush(stdout);
。
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.
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.
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;
}
Eclipse's terminal emulator might be different and do more buffering. Try calling fflush(stdout);
between the printout and the call to scanf()
.
这篇关于Eclipse / MiniGW中的scanf问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!