can anyone help with c++ code..........Write a program that dispenses change. The program should read the amount of the purchase and the amount paid and then display the number of dollars, quarters, dimes, nickels, and pennies given in change. (Hint: Convert the purchase amount and the amount paid to pennies.) Calculate the difference (changeDue). Use the % operator to determine how many dollars to pay. Subtract that number of pennies from changeDue and continue this process for quarters, dimes, etc. Your answer may be off by a penny. Do you have any idea why this might be the case?
If you work with pennies, you can do it all with integers, so i don't see how it could be off by a penny. What help do you need? In this case you can think about it just like you do in real life. If I need to give you $145.78 in change, and then hand you a bunch of cash, how do you check it? First, you look at the big bills (say 20s) and figure how many of those go into 145.78... so seven 20's. Then what's left? 7 20's is 140, and 145.78 minus 140 is 5.78 - so what's next? By converting to pennies first, you just multiply all your numbers by 100 (so 145.78 is 14578 and 20's are 2000's) and the math works the same way. Divide, get remainder, divide again. Repeat.
If you are using floats as an input and converting to pennies, you may lose precision. If you add the floats and then convert, you will most likely lose a penny here and there. As prices are input as a float as opposed to an integer...
But if you convert to pennies first thing (i.e. take their input, which is a string, and convert it to an integer) it should stay precise.
That is correct, but I was answering the question that was to state how the data could be imprecise.
Good point @ecdown.
thanks a lo
thanks a lot
Join our real-time social learning platform and learn together with your friends!