S
Shawn
Hello all,
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to problems with floating
point arithmetic and I am looking for a way to solve it. See the code
below...
Given a certain amount of change (below $1.00) the program will tell
you how many of each coin you will need to get that amount. The
program is "working" in that the logic appears correct and it DOES
work for some numbers, but for others, it is not. The problem appears
to be that 0.01 as I see it, is not being represented in memory.
I have done some searching online and I am sure this is a common
problem but I just can't seem to find the workaround...
Thank you for all your help...
Shawn
#include <stdio.h>
int main()
{
char input[100];
float total_input, running_total;
int quarters, nickels, dimes, pennies;
/* Zero out all the variables */
quarters = nickels = dimes = pennies = 0;
total_input = running_total = 0;
/* Get the necessary amount of change */
printf("Enter the total amount of change, less than $1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);
/* Loop until we get a sane amount */
while (total_input >= 1.00 || total_input <= 0.00) {
printf("Total is not between $0.00 and $1.00.\n");
printf("Enter the total amount of change, less than
$1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);
}
/* Store in another variable so we can use it */
running_total = total_input;
while (running_total >= 0.25) {
++quarters;
running_total -= 0.25;
}
while (running_total >= 0.10) {
++dimes;
running_total -= 0.10;
}
while (running_total >= 0.05) {
++nickels;
running_total -= 0.05;
}
while (running_total >= 0.01) {
++pennies;
running_total -= 0.01;
}
printf("In order to get $%.2f in change, you will need:\n",
total_input);
printf("%d quarters,\n", quarters);
printf("%d dimes,\n", dimes);
printf("%d nickels,\n", nickels);
printf("%d pennies\n", pennies);
return(0);
}
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to problems with floating
point arithmetic and I am looking for a way to solve it. See the code
below...
Given a certain amount of change (below $1.00) the program will tell
you how many of each coin you will need to get that amount. The
program is "working" in that the logic appears correct and it DOES
work for some numbers, but for others, it is not. The problem appears
to be that 0.01 as I see it, is not being represented in memory.
I have done some searching online and I am sure this is a common
problem but I just can't seem to find the workaround...
Thank you for all your help...
Shawn
#include <stdio.h>
int main()
{
char input[100];
float total_input, running_total;
int quarters, nickels, dimes, pennies;
/* Zero out all the variables */
quarters = nickels = dimes = pennies = 0;
total_input = running_total = 0;
/* Get the necessary amount of change */
printf("Enter the total amount of change, less than $1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);
/* Loop until we get a sane amount */
while (total_input >= 1.00 || total_input <= 0.00) {
printf("Total is not between $0.00 and $1.00.\n");
printf("Enter the total amount of change, less than
$1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);
}
/* Store in another variable so we can use it */
running_total = total_input;
while (running_total >= 0.25) {
++quarters;
running_total -= 0.25;
}
while (running_total >= 0.10) {
++dimes;
running_total -= 0.10;
}
while (running_total >= 0.05) {
++nickels;
running_total -= 0.05;
}
while (running_total >= 0.01) {
++pennies;
running_total -= 0.01;
}
printf("In order to get $%.2f in change, you will need:\n",
total_input);
printf("%d quarters,\n", quarters);
printf("%d dimes,\n", dimes);
printf("%d nickels,\n", nickels);
printf("%d pennies\n", pennies);
return(0);
}