Thread: Change maker program

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    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 */
    I know it's a mess but can some please help me make this work?
    Last edited by davidjg; 03-25-2011 at 03:54 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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
    Last edited by CommonTater; 03-25-2011 at 04:02 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    This is my output. Can anyone tell me why and help me out as to what I'm doing wrong?

    0.00Your total is: $0.00

    0.00
    0.00 Dollars
    0.000000
    0.00
    0.00
    0.000.00

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    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


    10.31Your total is: $20.62

    20.62
    20.62 Dollars
    0.00
    0.00
    0.00
    0.000.00

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You weren't entering 2, because otherwise you wouldn't have said this was your output:
    Quote Originally Posted by YOU
    0.00Your total is: $0.00

    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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?


    Quote Originally Posted by quzah View Post
    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.
    Last edited by CommonTater; 03-25-2011 at 04:22 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    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. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    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."
    Me "I already said that."
    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.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    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. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Smeg off!
    I get that a lot when I prove people wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with change return problem.
    By brian75 in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2009, 06:07 PM
  2. help with change program
    By f4ichick02 in forum C Programming
    Replies: 12
    Last Post: 10-21-2009, 02:49 PM
  3. Program to calculate change return.
    By OrAnGeWorX in forum C Programming
    Replies: 15
    Last Post: 11-17-2008, 09:49 AM
  4. type casting?
    By greatonesv in forum C Programming
    Replies: 12
    Last Post: 10-22-2008, 08:21 PM
  5. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM