gpt4 book ai didi

c - 帮助 : How to write this simple code according to the requirement?

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:54 26 4
gpt4 key购买 nike

问题:为模拟警用雷达枪的程序创建算法。该算法应读取汽车速度(以公里/小时为单位)并在速度超过 59 公里/小时时打印消息“超速”。然后,该算法还应计算出适当的罚款,超过限制 1-10kmh 罚款 80 美元,超过限制 11-30kmh 罚款 150 美元,超过限制 31kmh 或以上罚款 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);
}

这里的问题是它不会打印出消息“Speeding”。谁能帮帮我?

最佳答案

printf 在写入标准输出时被缓冲。在 printf 函数之后使用 fflush(stdout),或者添加一个新行,即 printf("Speeding\n");

您还可以使用 setbuf(stdout, NULL);

禁用 stdout 流上的缓冲

关于c - 帮助 : How to write this simple code according to the requirement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5203455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com