Hi
@jake
The problem here is the float point number. Since the world of computer is based on digital signal, the float number isn't 100% accurate as in the math.
To see this problem you can run your program and input 9.99 as sub, 10 as amount paid. Does the due number seems confusing? It's 0.0100002 on my computer. You can google that for more information.
To solve this problem, you can add EPSILON to avoid the residual, or you can use another way to achieve this, which is also my favourite way. Use the pennies as an example, replace the if code by something like:
1 2 3 4 5
|
while (changeamt>=penny)
{
pennies++;
changeamt-=penny;
}
|
You can do the same to the others. As you can see this piece of code contains no division, which is a painful operation for float number. The residual problem is sovled in the while's condition "changeamt >=penny".
I'm not sure if this will work 100% accurate, but it seems elegant, so should be working in least 90% of the situations.