I would really appreciate it if someone could help me with a problem i am facing, because seriously its driving me crazy.
My problem is as follows (withdraw part) : (i am using eclipse btw)
If i want to withdraw any amount that combines euro coins and fifty cent coins, or just euro or fifty coins, its all good.
But when i try to withdraw amounts such as 1.65 which requires all 4 types of coins or at least 3 types, it doesnt occur
If the coins in the piggybank is of 10 and 5 cent and try to withdraw the same amount it wont work at all.
actually when it comes to 5 and 10 cent... it will withdraw up to 0.20 cent if its 10 cent and up to 0.10 cent if its 5 cent
and also if its a combination of the two, 0.15 for example it wont work again.
It doesnt make sense though, because is the same logic i used for the fifty cent coins and it works perfectly and
on top of that why does it work up to a certain point only?
I dont have any comments on that section of the program yet, so here some tips.
1. the problem is on the "withdrawdetails" function. code line 215-310 (thats for the funtion) and code lines 254-266 (for the problem)
2.the variables "euro,fifty,ten,five" are for the counting of each coin that will be withdrawn
3.the variables "valideuro,validfifty,validten,validfive,validwithdraw", store the total amount of each coin type
(temporary, just in case the transaction cannot occur, you will understand why once you see the code)
4. the variable "withdrawal" is the amount of money the user wants to withdraw.
even so, why does it work partly? i mean, it works with 0.05 and 0.10 (when it comes to 5 cent) but it doesnt work with 0.15
and for the 10 cents, it works when it comes to 0.10 and 0.20 and it doesnt worl at 0.30
Trying to do momentary computations with floating point numbers is problematic because of very small roundoff errors inherent with floating point numbers. Remember that a computer stores values in binary and not all decimal numbers can be accurately expressed in binary. The link I provided above should really help explain this issue.
To fix your problem you would be better off storing your numbers in integral types instead of floating point numbers. Instead of using a floating point number to hold your currency convert everything to cents and then do only integer math on that amount.