You are doing the computation (int price_final = (price - (price - 0.01);
before you obtain the value of price
. This is how the code should be:
if (value == 1) {
float price = 0, price_final;
printf ("INSERT A PRICE >> ");
if (scanf ("%f", &price) == 0) {
fprintf(stderr, "Invalid price");
exit(EXIT_FAILURE);
}
price_final = price - (price - 0.01);
printf ("Final value %f\n", price_final);
}
You’re also subtracting a float
(0.01) from an int
, which is undefined, so I changed both variables to float
.
Also, I’d recommend that you read a line first with fgets
and parse the string using sscanf
:
char line[100];
fgets(line, 99, stdin);
int price;
if (sscanf(line, "%d", &price) == 0)
handle error