1

everyone. I NEED HELP! I was trying to submit this following HackerRank's challenge : Task Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. Round the result to the nearest integer.

#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;
}

After compiling the code above. I always get these errors :

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=]

What is the problem ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Cyclone
  • 25
  • 7

2 Answers2

1

Change mealc to float. At your second scanf tou missed a %: scanf("%d",&tip);

0

As the warning message says the conversion specifier %f is designated to input values for objects of the type float instead of the type double.

To input a value for an object of the type double you need to use the conversion specifier %lf.

scanf("%lf",&mealc);

Also you have a typo in this call

scanf("d",&tip);

you need to write

scanf("%d",&tip);

And in this statement

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

there is a redundant closing parenthesis. You need to write

mealc = mealc+(mealc*tip/100)+(mealc*tax/100);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • ` #include #include int main() { int tax,tip; double mealc; scanf("%lf",&mealc); scanf("%d",&tip); scanf("%d",&tax); mealc = mealc+(mealc*tip/100)+(mealc*tax/100); printf ("%f",round(mealc)); return 0; } ` Yeah, it worked fine, but the result is float type. When I change the type I get the same error. – Cyclone Nov 29 '21 at 22:02
  • If you're worried about the last printout containing a decimal point, either use `%.0f`, or maybe `%d` along with `(int)round(mealc)`. – Steve Summit Nov 29 '21 at 22:08
  • Thanks, it worked.. – Cyclone Nov 29 '21 at 22:14
  • What is the difference between `"%f"` and `"%lf"`? – Cyclone Nov 29 '21 at 22:20
  • @Cyclone Used with the function scanf the conversion specifier %f expects a pointer to an object of the type float while the conversion specifier %lf expects a pointer to an object of the type double. – Vlad from Moscow Nov 29 '21 at 22:24