This is what i need help with it works fine with Loonies,quarters,dimes,nickels but if i enter say 6.01 it tells me i need pennies but as you can see the .01 means theres 1 penny needed. And if i enter say 6.72 is says i need 1 penny instead of 2. how do i fix this
It is a problem with the way computers store floating point numbers. 6.72 is not stored exactly, so when you convert ittruncate it to an integer you loose information that you need.
I recommend that instead of doing everything with floating-point arithmetic, you first turn the number the user entered into an integer. Round properly in order to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
double dchange;
int change;
int loonies;
int quarters;
int dimes;
int nickles;
int pennies;
cout << "Please enter the amount of change due: ";
cin >> dchange;
// Convert to integer value, fixing for roundoff errors
change = (dchange * 100) + 0.5;
// Calculate the minimum number of loonies and coins
loonies = change / 100;
quarters = (change - (loonies * 100)) / 25;
...
BTW, your indentation needs help. You need to be very consistent about how you do it.
Also, you can use the ?: operator to avoid all those if statements: