3

Why am I getting this warning while trying to compile my program? %lf' expects argument of type 'double', but argument 2 has type 'double *' I'm using CodeBlocks IDE, But these lines give a huge number:

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

I'm new to C programming and still learning stuff.

// 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;
}
  • This code compiles fine for me on gcc 4.8.2., and works as expected. – Chris Beck Sep 13 '15 at 01:22
  • @ChrisBeck, but it gives a huge number when I run `double calculate1 = mealPrice * (double)(percentage)/100;`. `percentage` is a type of `int` –  Sep 13 '15 at 01:23
  • John, does it not compile, or does it give a huge number? Your post indicated a compiler error. Please post the complete code you are actually working with. – Chris Beck Sep 13 '15 at 01:26
  • @ChrisBeck, code is updated –  Sep 13 '15 at 01:29

1 Answers1

7

The problem is that you should not use pointers with printf (unless you are using pointer format specifiers). You generally pass things by pointer to scanf (because it needs to change them) and by value to printf (because it's not supposed to change them). That's why you are getting huge numbers.

It should look like:

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

and

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

not

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

or

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

Also in the future, don't show us compiler warnings unless they are specifically connected to the code that you posted, or you are going to get confused responses.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87
  • Thank you for your response, @ChrisBeck, but one more thing: when it asks to `"Enter the price of the meal:\n"` I have to enter numbers twice (separated by Enter) instead of only once. Please could you please explain that? –  Sep 13 '15 at 01:58
  • 1
    To be honest I think you should just follow the advice in this post: http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf – Chris Beck Sep 13 '15 at 02:01
  • 1
    Also here maybe: http://stackoverflow.com/questions/15443483/using-n-in-scanf-in-c – Chris Beck Sep 13 '15 at 02:05