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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//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?