Thread: Trying to convert a total amount into change

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    Trying to convert a total amount into change

    I'm trying to convert the total amount left over after a purchase into the correct amount of change. I have written a program but for some reason the dimes go insane and it randomly generates nickels. Can someone tell me why this isn't working?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    int main()
    {
        
           float amountdue;
             
           printf( "Please enter your amount due(in dollars): " );
           scanf( "&#37;f", &amountdue );
           printf( "You have entered %.2f dollars.\n", amountdue );
     
           float amountgiven;
     
           printf( "Please enter the amount given(in dollars): " );
           scanf( "%f", &amountgiven );
           printf( "You have entered %.2f dollars.\n", amountgiven );
           
           float remaining;
           
           remaining = amountgiven-amountdue;
           
           if ( remaining < 0 ) 
           {
                printf( "You do not have enough money!\n" );
           }
           float dollars;
           float quarters;
           float dimes;
           float nickels;
           float pennies;
           
           dollars = 0;
           quarters = 0;
           dimes = 0;
           nickels = 0;
           pennies = 0;
           
           printf ( "You will recieve %.2f back.", remaining );
           
           while ( remaining >= 1 ) {
                 remaining = ( remaining - 1 );
                 dollars = ( dollars + 1 );        
           }
           printf ( "Amounting to: %.0f dollars,", dollars );
           
           while ( remaining >= .25 ) {
                 remaining = ( remaining - .25 );
                 quarters = ( quarters + 1 );
           }
           
           printf ( " %.0f quarters,", quarters );
           
           while ( remaining >= .1 ) {
                 remaining = ( remaining - 0.1 );
                 dimes = ( dimes + 1 );
           }
           
           printf ( " %.0f dimes,", dimes  );
           
           while ( remaining >= .05 ) {
                 remaining = ( remaining - 0.05 );
                 nickels = ( nickels + 1 );
           }
           
           printf ( " %.0f nickels,", nickels );
           
           while ( remaining >= .01 ) {
                 remaining = ( remaining - 0.01 );
                 pennies = ( pennies + 1 );
           }
           
           printf ( " and %.0f pennies." , pennies );
                
           
           
          
            system("pause");
           
           exit (0);
     
    }
    Last edited by American Raptor; 12-07-2008 at 08:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you start with 2348756 dimes, and then you add maybe 2. So that gives you 2348758 dimes, rather than 2. Perhaps you want to start with 0 dimes?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    I corrected the problems, but it is off by 1 penny!
    Last edited by American Raptor; 12-07-2008 at 08:58 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Had you searched for the last time someone asked about making change, you would have found a discussion about precision and accuracy of floating point numbers. Most change amounts are not exactly representable inside the computer, and sometimes they come up short. For example, $1.68 is actually stored (assuming IEEE floats, but that's what everyone uses these days) as 1.10101110000101000111101 in binary, which is a little under 1.68, which means that when you get down to the last penny, remaining is actually <0.01 by a little bit, hence the last penny doesn't get added in.

    My suggestion would be to do all your calculations in integral numbers of pennies, rather than with floating point arithmetic (so you would start by multiplying all your dollar amounts by 100 and rounding to the nearest penny).

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Alright, sorry.

    Thanks for the help.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    For example, $1.68 is actually stored (assuming IEEE floats, but that's what everyone uses these days) as 1.10101110000101000111101 in binary, which is a little under 1.68, which means that when you get down to the last penny, remaining is actually <0.01 by a little bit, hence the last penny doesn't get added in.
    Isn't that crazy!!

    Money is really about integers anyway. But, NB. That division in C is always rounded down (eg, 11/4 = 2) which might not be fiscally responsible.

    edit: this means use modulus (&#37 for the pennies
    Code:
    int total=11, nickels=total/5, pennies=11%5;
    Nickels will be 2 and pennies will be 1.
    edit2: looking more closely, modulus is pretty much the key to change making:
    Code:
    int total=168, quarters=total/25, dimes=(total%25)/10, nickels=((total%25)%10)/5, pennies=((total%25)%10)%5;
    ...i think
    Last edited by MK27; 12-07-2008 at 09:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    68 C:\Users\***** ******\Documents\Test.cpp invalid operands of types `double ()(double, double)' and `int' to binary `operator&#37;'

    I recieve that error when I try that.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can only use % on integers. (Yet another reason for not using floating-point....)

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's what I meant:

    Code:
    #include <stdio.h>
    
    int main () {
    	int total=168, quarters=total/25, dimes=(total&#37;25)/10, 
                           nickels=((total%25)%10)/5, pennies=((total%25)%10)%5;
    	printf("%d %d %d %d\n",quarters,dimes,nickels,pennies);
    }
    Output:
    6 1 1 3
    That's $1.68
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Thanks, works perfect now that I had taken it apart and understand it :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program wont run, always aborts
    By stormfront in forum C Programming
    Replies: 31
    Last Post: 10-31-2005, 05:55 PM
  2. please help me with this loop
    By bliznags in forum C Programming
    Replies: 2
    Last Post: 03-21-2005, 08:57 AM
  3. I need some help debugging homework.
    By sillyrabbit in forum C Programming
    Replies: 7
    Last Post: 03-15-2004, 01:47 AM
  4. can someone check this code
    By xpflyer2002 in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM