| supersixone (13) | |||
|
Hello I'm working on this problem where the user inputs an amount of money between $0.00 and $19.99, and the program outputs the best way to calculate the change of that amount. So for example, if I put in "$9.94", then the program should output One $5 bill 4 $1 bills 3 quarters 1 dime 1 nickel 4 pennies I started doing this:
but I feel like its really inefficient and am wondering if you guys can point me in the right direction as to how to make this program. I can't think of a way to use loops in this, but if there is a way, will you let me know? Thanks for reading | |||
|
|
|||
| Disch (8349) | |
|
Yes that is very inefficient.. and it will require much more coding than a simpler alternative. 1) Use variables. Make a variable that counts how many of each bill/coin you need. 2) Use division (/) and mod (%) operators to easily find out how many coins you need. Note that mod only works with integers, so I could multiply by 100 to get rid of that floating point. hint: This should require exactly zero if statements. You should be able to do it all with math. | |
|
Last edited on
|
|
| supersixone (13) | |
|
ok thanks how would you implement the multiplying 100 part? Like for example, if I wanted to find the number of 10 dollar bills it had to use, tenDollar = total*100 % 10*100like that? | |
|
Last edited on
|
|
| EddieV223 (15) | |
|
I suggest writing a class that stores Money in ints. class Money { int mDollars; unsigned int mCents; // then fill in the required methods for manipulating this data. }; | |
|
|
|