问题:为一个模拟警察雷达枪的程序创建一个算法。该算法应读取汽车速度(以公里/小时为单位),如果速度超过59公里/小时,则打印消息“超速”。然后,算法还应计算适当的罚款,超过限制的1-10公里/小时为80美元,超过限制的11-30公里/小时为150美元,超过限制的31公里/小时或更多为500美元。使用下面的空格。
我的解决方案:
#include <stdio.h>
int main()
{
int speed = 0;
int limit = 59;
/*Get automobile speed from user*/
printf("Please enter the speed: ");
scanf("%d%*c", &speed);
/* Based on the speed, generates the corresponding fine*/
if (speed > limit)
{
printf("Speeding");
if((speed - limit) <= 10)
printf("Fine: $80");
else
if((speed - limit) >= 11 & (speed - limit) <= 30)
printf("Fine: $150");
else
if((speed - limit) >= 31)
printf("Fine: $500");
}
else
printf("The automobile is not speeding");
return (0);
}这里的问题是它不会打印出消息“超速”。有人能帮我一下吗?
发布于 2011-03-05 19:21:35
printf在写入标准输出时被缓冲。在您的printf函数之后使用fflush(stdout),或者添加新行,即printf("Speeding\n");
还可以使用setbuf(stdout, NULL);禁用标准输出流上的缓冲
https://stackoverflow.com/questions/5203455
复制相似问题