1. ## C Programming Question

I've been assigned to run a program to calculate change. I have to display how many dollars, quarters, dimes, etc are to be returned to the customer. I am leaving out half of the problem, but I'm not having any issues with that part. Just having issues figuring out how to show that the customers gets back 4 dollars, 2 quarters, & 1 dime, etc. Any help is much appreciated. Thanks

2. By leaving out what you have done, we have no way to help you. Around here, it always pays to show what you have so far. This tells us many things, like in general your level of expertise (so we don't shoot a solution to you far more advanced than the rest of your app), compiler, platform, etc.

What exactly are you having problems with? And if its just 'having issues' then there is no code to fix...

3. If they're all in separate variables, ie
Code:
`int dollars, quarters, dimes, nickels, pennies;`
then
Code:
`printf("%d dollars, %d quarters, %dimes, %d nickels, %d pennies\n", dollars, quarters, dimes, nickels, pennies);`
is what you're looking for.

But like jcobb said, if this isn't your answer then you're going to have to explain yourself a bit more.

4. This is what I have:

Code:
```#include <stdio.h>
int main()
{
float owe, paid, change;
int dollars, quarters, dimes, nickels, pennies;
printf("How much does the customer owe? ");
scanf("%f",&owe);
printf("How much did the customer pay? ");
scanf("%f",&paid);
change=paid-owe;
printf("The customers change is \$%5.2f\n",change);

return 0;
}```
So, I'm confused on how to display if the change is \$4.25, to show 4 dollars, 1 quarter

5. Well the first thing I would do is convert it to pennies and stick it into an integer by taking change, multiplying it by 100 and assign it to an int. In your case it would convert the float from 4.25 to 425. Then I push it through a decision tree where you would do something like intChange = 425; (note that is calculated). Then since a dollar unit is 100 pennies, do a totalDollars = intChange / 100; This will yield 4 in this case. Get the resulting pennies by doing remainingPennies = intChange - (totalDollars * 100). This will leave you with 25 in the remaining pennies var and 4 in the total dollars var. Then repeat the same basic process with quarters (25 pennies):
Code:
```totalQuarters = intChange / 25;
int remainingPennies = intChange - (totalQuarters * 25);

if( remainingPennies == 0)
{
cout << "we are done:" << endl;
cout << "Total change: Dollars: " << totalDollars << ", Quarters: " << totalQuarters << endl;
}```
I will leave the dimes etc as an exercise for the OP. It should be blindingly obvious by now. I know you could use the modulo operator to do the change thing too but given that the OP was confused by something like this my intent was to keep any formulas as simple at possible.

6. I would just plain not use floats. Use an integer value cents. Then this one is all just simple modulus and subtraction.

pay - owe = cents
cents -= (cents%25)*25 <- get quarters
cents -= (cents%10)*10 <- get dimes

Obviously the "getting" is left out of this big hint.

If you want to print your cents as \$:
Code:
`printf("\$%d.%02d",cents/100,cents%100);`

7. Originally Posted by MK27
I would just plain not use floats. Use an integer value cents. Then this one is all just simple modulus and subtraction.

pay - owe = cents
cents -= (cents%25)*25 <- get quarters
cents -= (cents%10)*10 <- get dimes

Obviously the "getting" is left out of this big hint.

If you want to print your cents as \$:
Code:
`printf("\$%d.%02d",cents/100,cents%100);`
100, 000.09999 percent agreed MK

floating point is good for some things but in far too many other cases it can lead to imprecise results.....

8. Is this the same assignment from this thread? Change function

9. its not the same, but close.

10. jeffcobb, did you notice this is the C board?
Posting C++ code in the C board and vice versa is usually not encouraged.
Another possible solution would be the way you do it in real life: Count the number of dollars you give back, then subtract that amount from what you have yet to give out, then from that, select the highest number of money type you give out, calculate the amount, etc.

11. You guys do realize that a thread on this particular assignment has been posted over 5 times in the past 2 months or so and that the code the OPs provide us with is a just a basic framework GIVEN BY THE INSTRUCTOR?!

12. Originally Posted by claudiu
You guys do realize that a thread on this particular assignment has been posted over 5 times in the past 2 months or so and that the code the OPs provide us with is a just a basic framework GIVEN BY THE INSTRUCTOR?!
Now you expect them to actually put in some effort on their own, and maybe even search the forums!? WHAT IS NONSENSE!?

Quzah.