我不知道需要做什么才能让我的代码正常工作,我尝试了一些方法,但同样的错误不断发生。我不知道如何继续,代码的主要问题似乎是将小时和分钟转换为仅小时。我知道这是一个非常基本的问题,但我是初学者,我似乎无法找到解决方案。
// freezer.c
// Estimates the temperature in a freezer given the elapsed time since a power failure.
#include <stdio.h>
#include <math.h>
int main(void) {
float dec_time, // input - time in hours and minutes.
temperature, // output - temperature in degrees celsius
hours, // input for dec_time
minutes; // input for dec_time
/* Get the time in hours and minutes */
printf("hours and minutes since power failure: ");
scanf("%lf%lf", &hours &minutes);
/* Convert the time in hours and minutes into only hours in real number */
dec_time = hours + (minutes / 60.0);
// Using time via an equation to estimate the temperature
temperature = ((4 * dec_time * dec_time) / (dec_time + 2)) - 20;
// Display the temperature in degrees celsius
printf("Temperature in freezer %9.2f.\n", temperature);
return 0;
}
任何人都可以给出任何解释,让我对此有更深入的了解,我将不胜感激。
编辑:当我将逗号添加到我的计算机上代码中的 scanf()
语句中时,标题中的主要编译错误已解决。我还将 %lf
更改为 %f
,但现在当我在 a.out
中键入一个数字时,例如3、直到我输入 q
! 后,程序才会计算。
最佳答案
将 scanf("%lf%lf", &小时 &分钟)
更改为 scanf("%f%f", &小时, &分钟)
。没有'l'
,@melpomene添加逗号 @Anton Malyshev 。
还建议检查结果是否为 2。(已成功扫描 2 个字段)。
if (2 != scanf("%f%f", &hours, &minutes)) {
puts("Input error");
exit(1);
}
关于c - 二进制 & 的操作数无效(有 'float *' 和 'float' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32145151/