# Change maker program

This is a discussion on Change maker program within the C Programming forums, part of the General Programming Boards category; Hello all: I am trying to make my change maker calculate the correct amount of change given and its not ...

1. Hello all:
I am trying to make my change maker calculate the correct amount of change given and its not calculating at all. My program is compiling and running but I cannot seem to find the issue with my algorithm. Can anyone provide some feed back as to what I'm doing wrong and point me in the right direction?

Code:
#include <stdio.h>
#include <math.h>

/* structure definitions */
struct currency {
float pennies, nickels, dimes, quarters;
int dollars;
}; /* end struct currency */

typedef struct currency Currency;

int main()
{
int userPayment, changeAmount, gallons, displayAmount;
float value;
float total;
Currency money;
money.pennies = 0.01;
money.nickels = 0.05;
money.dimes = 0.10;
money.quarters = 0.25;
money.dollars = 1.00;
float unitp;
float amount;

printf( "Welcome to Dave's paint production. How many gallons would you "
"like to order?\nThe unit price is \$10.31 a gallon.\n" );
scanf( "%d", &gallons );

printf( "You have chosen to purhase: %d\n", gallons );
printf( "at 10.31 per gallon\n\n" );

unitp = 10.31;

printf("\n %.2f", unitp);

total = ( gallons * unitp );

printf( "Your total is: \$%.2f\n", total );
value = total;
amount = value;

printf( "\n %.2f", value );
while ( value > 0 ){
amount = amount / 1;
value = value - amount;
printf( "\n%.2f Dollars", amount );
printf( "\n%2f", value );

amount = value / 0.25;
value = value - (amount * 0.25 );
if ( amount > 0 )
printf( "\n%.2f Quarters", amount );
printf( "\n%.2f", value );

amount = value / 0.10;
value = value - (amount * 0.10 );
if ( amount > 0 )
printf( "\n%.2f Dimes", amount );
printf( "\n%.2f", value );

amount = value / 0.01;
value = value - (amount * 0.01 );
if ( amount > 0 )
printf( "\n%.2f Pennies", amount );
printf( "\n%.2f", value );

//amount = value / 0.1;
printf( "%.2f", amount );
/*
value = value - ( amount * 00.1 );
if ( amount > 0 )
printf( "\n%f Dimes", amount );

*/

} /* end while */

//printf( "How much money do you give for payment?\n" );
//scanf( "%.2f\n", &userPayment);

/*
printf( "The amount you entered is less than you need to pay. Please try again.\n" );
scanf( "%3.2f\n", &userPayment);

printf( "The total amount you need to pay is: "%d", &userPayment" );
(M1.pennies, M1.nickels, M1.dimes, M1.quarters, M1.dollars);

*/

return 0; /* indicates successful termination */
} /* end main */

2. You need to do some searching on these forums.

This problem has been posted here several times over the past few months and some pretty good solutions have been presented... in particular look for the ones that use integers and work in pennies, not floating point dollars.

Here's one example.... Help figuring out why calculations are not working correctly

3. This is my output. Can anyone tell me why and help me out as to what I'm doing wrong?

0.00
0.00 Dollars
0.000000
0.00
0.00
0.000.00

4. Without seeing the line above that, I can only assume that you are entering 0 for the number of gallons you want to purchase.

Quzah.

5. No. I'm actually entering 2 for the gallons.

./a.out
Welcome to Dave's paint production. How many gallons would you like to order?
The unit price is \$10.31 a gallon.
2
You have chosen to purhase: 2
at 10.31 per gallon

20.62
20.62 Dollars
0.00
0.00
0.00
0.000.00

6. You weren't entering 2, because otherwise you wouldn't have said this was your output:
Originally Posted by YOU

0.00
0.00 Dollars

0.000000
0.00
0.00
0.000.00
As for the rest, your math really isn't where it should be on this:
Code:
if total > 99 cents
subtract all the dollars
if total > 50 cents
subtract a 50 cent piece
... repeat until you have no more change
Like 'tater said, this program comes up all the time here. How about searching the forum for one of the many posts on it and see how they did it?

Quzah.

7. Ok lets look at some of your math...

Code:
amount  = value;
amount = amount / 1;
value = value - amount;
Lets say we have \$2.00 ... what is 2 /1 ? Yep 2 ... so both amount and value = 2
Now you say value = 2 - 2 ... so right away value = 0;

So what's this going to print?
Code:
printf( "\n%.2f Dollars", amount );
printf( "\n%2f", value );
Next we come into the second calculation...
Code:
amount = value / 0.25;
value = value - (amount * 0.25 );
What is the value of value of value when you entered this block? Yep, that would be 0.

So what is 0 / .25 ? so amount ends up being 0
What is 0 - (0 *.25)? Yep still 0...

So then what happens next with amount = 0?
Code:
if ( amount > 0 )
printf( "\n%.2f Quarters", amount );
printf( "\n%.2f", value );
Now track that through the following steps...
Code:
amount = value / 0.10;
value = value - (amount * 0.10 );
if ( amount > 0 )
printf( "\n%.2f Dimes", amount );
printf( "\n%.2f", value );

amount = value / 0.01;
value = value - (amount * 0.01 );
if ( amount > 0 )
printf( "\n%.2f Pennies", amount );
printf( "\n%.2f", value );

//amount = value / 0.1;
printf( "%.2f", amount );
/*
value = value - ( amount * 00.1 );
if ( amount > 0 )
printf( "\n%f Dimes", amount );
See the problem now?

Originally Posted by quzah
Without seeing the line above that, I can only assume that you are entering 0 for the number of gallons you want to purchase.

Quzah.
Follow his math... it won't matter what he enters.

8. Originally Posted by CommonTater
Follow his math... it won't matter what he enters.
I wasn't disputing that. I was disputing his total line as I showed above. If his total was zero, it's because he's multiplying by zero.

Quzah.

9. Originally Posted by quzah
I wasn't disputing that. I was disputing his total line as I showed above. If his total was zero, it's because he's multiplying by zero.

Quzah.
Yep... but it won't matter how much he enters... it will always be 0, right after that first calculation.

10. How many times do you want me to say "I wasn't disputing that" before you understand what I'm saying? I said in my first post that his math was off, aside from him entering 0:
As for the rest, your math really isn't where it should be on this:
You really have a problem with that whole reading thing sometimes.

Quzah.

11. Originally Posted by quzah
How many times do you want me to say "I wasn't disputing that" before you understand what I'm saying? I said in my first post that his math was off, aside from him entering 0:You really have a problem with that whole reading thing sometimes.
Quzah.
And how many times does he have to post a correction before you deem to see it?
GEES...

12. Originally Posted by CommonTater
And how many times does he have to post a correction before you deem to see it?
You're a few fries short of a Happy Meal.

OP "I'm getting this, why!"
Me "You entered zero."
OP "No I didn't."
Me "Yes you did, otherwise you wouldn't have had the output you said you had. Besides that, the rest of your math is wrong."
You "His math is wrong."
You "His math is wrong."
Me "Yeah, I said that."

If it's possible to study to improve reading comprehension, you should go do it, because you are absolutely terrible at it.

Quzah.

13. Originally Posted by quzah
If it's possible to study to improve reading comprehension, you should go do it, because you are absolutely terrible at it.
Quzah.
Smeg off!

14. Originally Posted by CommonTater
Smeg off!
I get that a lot when I prove people wrong.

Quzah.

15. Thank you for all of the help. I was able to resolve the issue and you were correct. The logic and the math was wrong.