# Thread: Change Maker (problems with %)

1. ## Change Maker (problems with %)

OK, so for school, I have to make a program that illustrates the change a person should get based on a few monetary inputs. I'm crafting one that does so in the following steps:

1. User inputs monetary value (e.g. 17.34 for \$17.34) describing the original bill.

2. User inputs monetary value describing how much cash they paid.

3a. If the amount paid is less than the bill, it tells you that you still owe the difference (which is produced).

3b. If the amount paid is more than the bill, it illustrates the most efficient change you should get, in dollars, quarters, dimes, nickels, and pennies.

3b is where I'm stuck. I know that inputting, for example, (25 % 3) would return the remainder of 25 / 3, which is 1. However, I want to divide, say, 25 by 3, return the 8, and keep the one to work with in the next step.

This is confusing me. D:

2. To get the eight, you would simply use integer math, ie 25 / 3 produces 8. 25 % 3 produces the one. Simply save them or whatever and continue processing.
If you cannot grasp how to do that, then you need to write down the logical steps needed to perform that. Imagine someone owing you something, for example, and think logically how such transactions would be done.
Btw, don't use goto.

3. Originally Posted by Elysia
Btw, don't use goto.
I SAID NOT TO TERRORIZE ME! :'(

So using the variables as integers in division just rounds them to the nearest whole number?

4. More precisely, it truncates the decimals. Eg 11 / 6 = 1.

5. Oh, so it always rounds down?
OK I'll give it a shot.

6. No, it discards the decimals. Chops them off.
Not quite the same thing, but I suppose you could say that.

7. I see what you mean.

So I say:
Code:
`cout << setprecision(0) << fixed << ((paid - bill) % 1);`
?
I assume that setprecision(0) would chop off the decimals as well. Kind of easier to manage how many decimal places for each statement individually, right?

Would I use % or / 1?

The for the next one, I'm not sure how to call in the leftovers from the last function.

8. There is no need to use % 1 or / 1 at all. They will not do anything.
The for the next one, I'm not sure how to call in the leftovers from the last function.
You will need to formulate proper logic for it first. Eg, how would you do it, in real life?

9. I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.

10. Not getting the objects or statements, or being really new isn't much of an excuse though. People who say things like that eventually get used to giving up at the slightest hardship and not doing anything. Not to mention this problem is entirely math based: if you paid \$20 for a 17.34 bill then you could get \$2, 2 quarters, 1 dime, 1 nickel, and 1 penny back. What did I really divide to get that, and why would you want to go on to a more difficult problem than this one, without figuring it out?

You ask how you want to save the result of 25 / 3 or 25 % 3. What are variables for?

11. Originally Posted by muffinman8641
I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.
If you really don't understand what they are telling you, try to understand that.... You can't live with the knowledge you gain in your class rooms.... Try to get your own.... Internet is the best source of getting knowledge......

Good luck..

12. % 1 gives the remainder of dividing something by one, which of course is always zero.

13. Originally Posted by muffinman8641
I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.
All I'm saying is this:
Before getting into statements and objects and whatnot, you should think of how the program should behave. Ie, how do you WANT it to behave? The logical part.
Then we can get into translating the logical part into statements and objects.

14. OK, sorry if I came off as an ass there.

I need to take the difference between the paid amount and the bill, then break it down into dollars and coins.

I need to say "How many whole dollars fit into this?"
Then subtract the dollars and say "How many quarters are in the remains?"
etc.

[Added note- I tried the suggestion of
Code:
```int x, y;
double a, b;```
for splitting up variable types, and it seemed to work ok. I'm just stuck on how to get a whole number with the decimal chopped off, because bill and paid are both originally double variables, since there's change involved with both. I tried making a new int variable called intdollars = (paid - bill), but I got a warning about that and it returned -2 billion dollars when I tested it.]

15. The reason will be that paid and bill will be double datatype variables and double datatype occupies 8 bytes in the memory (C++), while integer just occupies 4 bytes (C++), so it truncates or process something differently while performing that operation...

You must declare dollars variable as double datatype and then you can format the decimal point using flags or setprecision....
Or
Code:
```double num = 23.345;
int intpart = (int)num;```