%LF'期望类型' double'的参数,但参数2的类型为' double *'

时间:2015-09-13 01:15:53

标签: c

为什么我在尝试编译程序时收到此警告? %lf' expects argument of type 'double', but argument 2 has type '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;
}

1 个答案:

答案 0 :(得分:4)

问题是你不应该使用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);

同样在将来,不要向我们展示编译器警告,除非它们与您发布的代码具体相关,否则您将会得到混淆的回复。

相关问题