# Thread: Trying to convert a total amount into change

1. ## Trying to convert a total amount into change

I'm trying to convert the total amount left over after a purchase into the correct amount of change. I have written a program but for some reason the dimes go insane and it randomly generates nickels. Can someone tell me why this isn't working?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

float amountdue;

scanf( "&#37;f", &amountdue );
printf( "You have entered %.2f dollars.\n", amountdue );

float amountgiven;

printf( "Please enter the amount given(in dollars): " );
scanf( "%f", &amountgiven );
printf( "You have entered %.2f dollars.\n", amountgiven );

float remaining;

remaining = amountgiven-amountdue;

if ( remaining < 0 )
{
printf( "You do not have enough money!\n" );
}
float dollars;
float quarters;
float dimes;
float nickels;
float pennies;

dollars = 0;
quarters = 0;
dimes = 0;
nickels = 0;
pennies = 0;

printf ( "You will recieve %.2f back.", remaining );

while ( remaining >= 1 ) {
remaining = ( remaining - 1 );
dollars = ( dollars + 1 );
}
printf ( "Amounting to: %.0f dollars,", dollars );

while ( remaining >= .25 ) {
remaining = ( remaining - .25 );
quarters = ( quarters + 1 );
}

printf ( " %.0f quarters,", quarters );

while ( remaining >= .1 ) {
remaining = ( remaining - 0.1 );
dimes = ( dimes + 1 );
}

printf ( " %.0f dimes,", dimes  );

while ( remaining >= .05 ) {
remaining = ( remaining - 0.05 );
nickels = ( nickels + 1 );
}

printf ( " %.0f nickels,", nickels );

while ( remaining >= .01 ) {
remaining = ( remaining - 0.01 );
pennies = ( pennies + 1 );
}

printf ( " and %.0f pennies." , pennies );

system("pause");

exit (0);

}```

2. So you start with 2348756 dimes, and then you add maybe 2. So that gives you 2348758 dimes, rather than 2. Perhaps you want to start with 0 dimes?

3. I corrected the problems, but it is off by 1 penny!

4. Had you searched for the last time someone asked about making change, you would have found a discussion about precision and accuracy of floating point numbers. Most change amounts are not exactly representable inside the computer, and sometimes they come up short. For example, \$1.68 is actually stored (assuming IEEE floats, but that's what everyone uses these days) as 1.10101110000101000111101 in binary, which is a little under 1.68, which means that when you get down to the last penny, remaining is actually <0.01 by a little bit, hence the last penny doesn't get added in.

My suggestion would be to do all your calculations in integral numbers of pennies, rather than with floating point arithmetic (so you would start by multiplying all your dollar amounts by 100 and rounding to the nearest penny).

5. Alright, sorry.

Thanks for the help.

6. Originally Posted by tabstop
For example, \$1.68 is actually stored (assuming IEEE floats, but that's what everyone uses these days) as 1.10101110000101000111101 in binary, which is a little under 1.68, which means that when you get down to the last penny, remaining is actually <0.01 by a little bit, hence the last penny doesn't get added in.
Isn't that crazy!!

Money is really about integers anyway. But, NB. That division in C is always rounded down (eg, 11/4 = 2) which might not be fiscally responsible.

edit: this means use modulus (&#37 for the pennies
Code:
`int total=11, nickels=total/5, pennies=11%5;`
Nickels will be 2 and pennies will be 1.
edit2: looking more closely, modulus is pretty much the key to change making:
Code:
`int total=168, quarters=total/25, dimes=(total%25)/10, nickels=((total%25)%10)/5, pennies=((total%25)%10)%5;`
...i think

7. 68 C:\Users\***** ******\Documents\Test.cpp invalid operands of types `double ()(double, double)' and `int' to binary `operator&#37;'

I recieve that error when I try that.

8. You can only use % on integers. (Yet another reason for not using floating-point....)

9. Here's what I meant:

Code:
```#include <stdio.h>

int main () {
int total=168, quarters=total/25, dimes=(total&#37;25)/10,
nickels=((total%25)%10)/5, pennies=((total%25)%10)%5;
printf("%d %d %d %d\n",quarters,dimes,nickels,pennies);
}```
Output:
6 1 1 3
That's \$1.68

10. Thanks, works perfect now that I had taken it apart and understand it :P