gpt4 book ai didi

c - printf() 函数出错

转载 作者:行者123 更新时间:2023-11-30 21:31:39 24 4
gpt4 key购买 nike

#include <stdio.h>

#define MINUTES_IN_HOUR 60
int main() {
// Two trains will pass each other at a certain time whne they are at an initial fixed distance apart.
// When will they pass each other and how much distance did each train have to travel?
// 1 corresponds to train 1, and 2 corresponds to train 2.

float distance_1_mile, distance_2_mile, distance_total_mile, rate_1_mph, rate_2_mph, time_minute;

time_minute = (distance_total_mile * MINUTES_IN_HOUR) / (rate_1_mph + rate_2_mph);
distance_1_mile = rate_1_mph * time_minute / MINUTES_IN_HOUR;
distance_2_mile = rate_2_mph * time_minute / MINUTES_IN_HOUR;

printf("The rate of train 1 and train 2 respectively are what values?\n");
scanf("%f%f", &rate_1_mph, &rate_2_mph);

printf("What was the total distance the trains were apart initially?\n");
scanf("%f", &distance_total_mile);

printf("The time it takes both trains to arrive side-by-side is %.3f minutes.\n", time_minute);

printf("The distance train 1 had to travel was %.2f miles.\n", distance_1_mile);
printf("The distance train 2 had to travel was %.2f miles.\n", distance_2_mile);

return 0;
}

我收到的错误是:“警告:格式“%f”需要“double”类型的参数,但参数 2 的类型为“int *”

我尝试在这个网站上查找这个问题,我看到同一问题有两种不同的解决方式,但似乎都不起作用。我也没有发布完整的代码,因为我看到的另一个例子也没有发布,并且他们的问题得到了回答。

最佳答案

编译器告诉你问题是什么,我假设你在某个地方有一行看起来像这样的行

int rate_1_mph, rate_2_mph, distance_total_mile, time_minute;

将 int 更改为 float。

完成此操作后,您需要将 printf 语句中的所有 %d 更改为 %f。 %d 实际上用于整数,而不是 double 。

关于c - printf() 函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890747/

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