Ahmed Talib
Ahmed Talib

Reputation: 7

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

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

Upvotes: 0

Views: 186

Answers (2)

Aaron
Aaron

Reputation: 3

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.

Upvotes: 1

Ahmed Talib
Ahmed Talib

Reputation: 7

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

Upvotes: -2

Related Questions