I am trying to make the double change; display what it is equal to in terms of change due in dollars, quarters, dimes, nickels and pennies.
Here is an example of what my code does and what I want it to do in the end. Price of Item: $15.00 Amount Payed: $20.55 Your change is: $5.55 That is: 5 dollars, 2 quarters, 0 dimes, 1 nickel, and 0 pennies.
(^^^What I want it to do^^^) I don't know how I could make it display the change in that format when change could technically be given in multiple ways.
This thread and that thread are not related. This is a different project. Read my whole post and you will understand. I am trying to display the change in digital currency.
I can see why you may have related the two because they both deal with money and I figure you probably saw this "cout << "This reads: 10 Dollars, 1 Quarter, 1 Dime, 1 Nickel, and 10 Pennies. Which equals, $10.50" << endl;". <-- This was only an output for users to understand how to input the values I wanted them to input. It wasn't a formula that converts.
Here is another example:
Change = $5.43
That is: 5 dollar(s), 1 quarter(s), 1 dime(s), 1 nickel(s), and 3 pennie(s)
The bold statement is what I would like to display based off of a function that will convert the change value into a format like the one in bold.
This is a classic programming problem which I know under the name
making change'. With American coins you are lucky and can use a greedy algorithm - just take the biggest coins that fit first and then move to smaller ones. With 12-cent coins added in, the greedy algorithm will not generate the fewest number of coins in all cases, though.
If you only need real US coin support, then use a greedy algorithm - just keep adding larger coins/bills first until it overflows, then move to smaller ones.
I prefer the cent method where 1 dollar = 100 pennies, a quarter = 25 ect..
Though you could use the decimal way like you currently are where 1 dollar = 1 dollar, 1 quarter = .25 dollar ect..
You need to remove the largest amounts first until you run out of money.
I find it easier to work with integers for this.
Yes, you should not use floating point types when dealing with currencies. You will get sued for millions one day due to floating point rounding errors.
Multiplying by 1 would be a bit redundant. I would personally parse it into cent to make it 100% accurate instead of the flawed floating point method. To get change is actually a pretty effortless task with stringstreams. I would personally mimic doubles like this.
Ps there may be bugs or something you don't like in the code, I was just showing an example how to do all integers with the appearance of doubles if you didn't want to have the user enter it in cents. If you had the user enter as cents 15$ = 1500
Okay so last night after doing the math for awhile I made an algorithm that can make my question possible. @LB as far as your advise to not use floats, I now see why. I noticed that about 5% of the time memory will round the pennies up by 1. I found two ways to solve my problem. One was to use division and mods, and the second was through this algorithm. Here is my code:
It can still be slightly off since you are using doubles. 1 could be represented as .999999999999999 or 1.00000000000000000001 or some other arbitrary value.