1

每个人。我需要帮助!我试图在 HackerRank 的挑战之后提交这个: 任务给定膳食价格(一顿饭的基本成本)、小费百分比(作为小费添加的膳食价格的百分比)和税收百分比(添加的膳食价格的百分比作为税金)对于一顿饭,查找并打印这顿饭的总成本。将结果四舍五入到最接近的整数。

#include <stdio.h>
#include <math.h>
int main()

{
    int tax,tip;
    double mealc;
    
scanf("%f",&mealc);
scanf("d",&tip);
scanf("%d",&tax);
mealc = mealc+(mealc*tip/100))+(mealc*tax/100);
printf ("%d",round(mealc));

    return 0;
}

编译上面的代码后。我总是收到这些错误:

Hk2.c:33:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]

Hk2.c:37:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

问题是什么 ?

4

2 回答 2

1

将 mealc 更改为浮动。在你第二次 scanf 时,你错过了一个 %: scanf("%d",&tip);

于 2021-11-29T21:23:22.007 回答
0

正如警告消息所说,转换说明符%f被指定为 type 对象的输入值,float而不是 type double

double要为需要使用转换说明符的类型的对象输入值%lf

scanf("%lf",&mealc);

你在这个电话中也有错字

scanf("d",&tip);

你需要写

scanf("%d",&tip);

在这个声明中

mealc = mealc+(mealc*tip/100))+(mealc*tax/100);

有一个多余的右括号。你需要写

mealc = mealc+(mealc*tip/100)+(mealc*tax/100);
于 2021-11-29T21:34:47.963 回答