gpt4 book ai didi

c - %lf' 需要类型为 'double' 的参数,但参数 2 的类型为 'double *'

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:18 24 4
gpt4 key购买 nike

为什么我在尝试编译我的程序时收到此警告?%lf' 需要“double”类型的参数,但参数 2 的类型为“double *”我正在使用 CodeBlocks IDE,但这些行给出了一个巨大的数字:

double calculate1 = mealPrice * (double)(percentage)/100;
printf("The tip that you should leave is %lf \n", &calculate1);

我是 C 编程的新手,仍在学习东西。

// CS 262, Lab Section <208>
// Lab 2

#include <stdio.h>
#include <stdlib.h>

int main(){
printf("Enter the price of the meal: \n");
double mealPrice = 0;
scanf("%lf\n", &mealPrice);

printf("Now enter the tip percentage: \n");
int percentage = 0;
scanf("%d\n", &percentage);


//Calculates tip amount in double, int, and float types
double calculate1 = mealPrice * (double)(percentage)/100;
printf("The tip that you should leave is %lf \n", &calculate1);
int calculate2 = (int)mealPrice * (int)(percentage/100);
printf("The tip that you should leave is %d\n", &calculate2);
float calculate3 = (float)mealPrice * (float)(percentage/100);
printf("The tip that you should leave is &f\n", &calculate3);

//Add tip to meal price
double total = calculate1 + mealPrice;
printf("The total price including tips is %lf\n", total);

printf("The meal cost is %f\nThe tip percentage is %d\nThe tip amount is%lf\nThe total cost is %lf\n", &mealPrice, &percentage, &calculate1, &total);
return 0;
}

最佳答案

问题是您不应该将指针与 printf 一起使用(除非您正在使用指针格式说明符)。您通常通过指向 scanf 的指针传递事物(因为它需要更改它们)并通过值传递给 printf(因为它不应该更改它们)。这就是为什么你会得到巨大的数字。

它应该是这样的:

printf("The tip that you should leave is %lf \n", calculate1);

scanf("%lf\n", &mealPrice);

不是

printf("The tip that you should leave is %lf \n", &calculate1);

scanf("%lf\n", mealPrice);

此外,以后不要向我们显示编译器警告,除非它们与您发布的代码特别相关,否则您会得到困惑的响应。

关于c - %lf' 需要类型为 'double' 的参数,但参数 2 的类型为 'double *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545383/

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