Home > Blockchain >  Why does using this code generates this error and how to fix it?
Why does using this code generates this error and how to fix it?

Time:08-15

The error I get is

error: passing float to parameter of incompatible type 'const char *' printf(finalprice, "%f\n");


#include <stdio.h>
#include <cs50.h>
float finalprice(float price, float offer){
    float finalprice = (price - offer) / 100;
    printf(finalprice,"%f\n");
}
int main(void)
{
    float price = get_float("Enter price: \n");
    float offer = get_float("Enter discount amount: \n");
    finalprice(price,offer);
}

I'm using the cs50.h library to the get_float() function is already predefined

CodePudding user response:

Instead of: printf(finalprice,"%f\n");

Try: printf("%f\n",finalprice);

On a side note ... when dealing with floats/doubles:

Instead of: float finalprice = (price - offer) / 100; Use: float finalprice = (price - offer) / 100.0;

The 100.0 stresses a 'real' number formatting, as opposed to an integer, and makes it more obvious to the programmer.

CodePudding user response:

I solved it, I had to redefine the finalprice1 variable inside the main function, don't know why but it worked

enter image description here

  • Related