You are using a version without Ads of this website. Please, consider donating:

### Precision Errors in a Change Calculator

Hello folks, so the assignment in question is to create a program that accepts two values (amount owed, amount paid) and returns a change value along with the correct amounts of dollars and coins that would make up that change.

My issue is that, in certain cases, the coin output seems to lose a penny! I've been messing around with the code for a few hours now and I can't get it to work. Here's the code without any alterations that presents the issue:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include #include using namespace std; int main () { double amountOwed = 0.0; double amountPaid = 0.0; restart: cout << "Enter the amount owed: \$"; cin >> amountOwed; cout << "Enter the amount paid: \$"; cin >> amountPaid; if (amountOwed > amountPaid) { cout << "You owe more money than that! Try again."; cout << endl << endl; goto restart; } if (amountOwed == amountPaid) { cout << "No change needed. Bye!"; cout << endl << endl; goto restart; } double change = 0.0; change = amountPaid - amountOwed; cout << endl << "The change for this transaction is: \$" << setprecision(2) << fixed << change << endl << endl; int dollars = 0; int quarters = 0; int dimes = 0; int nickels = 0; int pennies = 0; dollars = change / 1; change = change - dollars; quarters = change / .25; change = change - (quarters * .25); dimes = change / .10; change = change - (dimes * .10); nickels = change / .05; change = change - (nickels * .05); pennies = change / .01; change = change - (pennies * .01); cout << "Dollars: " << dollars << endl; cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickels << endl; cout << "Pennies: " << pennies << endl; cout << endl; goto restart; return 0; }``````

I believe the issue arises due to the use of a double value (change) inside calculations for int values, but I can't seem to correct it. I tried converting the change value into an integer value and multiplying by it and the values inside the dollar/coin values by 100, but the issue persisted. I also tried using this:

 ``123456789101112131415`` ``````//change has been reduced to penny-level by previous calculations if (change / .01 > 1) { pennies = change / .01; change = change - (pennies * .01); if (abs(change - .01) < .001 && abs(change - .01) != 0) pennies ++; if (pennies == 5) { pennies = 0; nickels ++; } }``````

But I still got the same vanishing penny errors (\$.11 change worked, but \$.16 resulted in 1 dime and 1 nickel, no pennies).

Any help is greatly appreciated! I've still got a few days to get this done and in the mean time I'll be able to talk with the professor, but I figured it couldn't hurt to get some outside help seeing as he can be a bit rough with forcing us to figure out issues like this.

On an unrelated note, is there a way to break up an inputted string into its individual characters?

Thanks!
Last edited on
 ``12345678910111213141516`` ``````#include #include using namespace std; int main () { string hello = "hello"; for(int i = 0 ; i < 5 ; i++) { cout << hello[i]; } system("PAUSE"); return 0; }``````
 `` `` ``helloPress any key to continue . . .``