This is a few lines of prototype code for a program that prints a restaurant menu and records the orders of customers. When I type 15.6 (just a random number I decided to test, it doesn't work for some other numbers too) into the scanf function, it does not seem to store the correct value in the respective variable (in this case, n).
I tried to change n into a float variable but it didn't solve anything. When I do calculations with the n value, the program seems to no longer work properly, regardless of the precision. I created another variable called change which equals to n - total. Theoretically, change should equal to 4.3 when n is 15.6. However, this program prints out "yes" then "no" instead. Is there a way to fix this problem?
void pay () {
double n, change, total = 11.3;
printf("\nPlease pay $%0.2f\n", total);
scanf("%lf", &n);
printf("%0.6f\n", n);
if (n == 15.6) {
printf("yes\n");
if (n >= total) {
change = n - total;
printf("%0.6f\n", change);
if (change == 4.3) {
printf("yes\n");
} else {
printf("no\n");
}
}
} else {
printf("no\n");
}
}