#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    newb problem, "Change machine" type of prog


    User types amount of money, program outputs how many quarters/dimes/nickels/penny's:

    Code:
    #include <stdio.h> 
    
    int main()
    {
    	float money, remain; 
    	unsigned int quarters, dimes, nickels, cents; 
    	const float QUARTER=0.25, DIME=0.10, NICKEL=0.05, PENNY=0.01; 
    	
    	do 
    	{
    		printf("Enter an amount of money: $"); 
    		scanf("%f",&money); 
    	} while(money < 0); 
    
    	printf("You've entered: $%.2f\n", money); 
    	
    	//Divide by .25 to get quarters, remainder / .10 
    	//for dimes, remainder by .05 for nickels 
    
    	quarters = money/QUARTER; 
    	printf("Quarters: %u\n", quarters); 
    	//printf("Dimes   : %u\n", dimes); 
    	//printf("Nickels : %u\n", nickels); 
    	//printf("Penny's : %u\n", cents); 
    	return 0; 
    }
    I can do it on paper (and in vb I think), but in C, % only work's with integer's, After getting quarters, I want remain to hold the amount of $ that's remainded, then I ca divide that by .10 for dimes and keep going..etc, How?

    Also I am getting 'c:\CPPCODE\C\MoneyChangeProgram\main.c(7): warning C4305: 'initializing' : truncation from 'double' to 'float'
    ' for the const float line, Why?
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12

    Re: newb problem, "Change machine" type of prog


    Originally posted by movEAX_444 Also I am getting 'c:\CPPCODE\C\MoneyChangeProgram\main.c(7): warning C4305: 'initializing' : truncation from 'double' to 'float'
    ' for the const float line, Why?
    By default, a floating point literal is of type double, which means that you're initialising floats with doubles. If you want the literals to be floats add an 'f' suffix, e.g. const float QUARTER=0.25f.

    There are two issues, firstly a possible loss of precision. Secondly, if the value of the double is out of range for a float, the resulting value is undefined (not a problem in your case).
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Is this what you are trying to do?


    It always gets the pennies off by one, likely because the actual representation of the amount is off by a tiny fraction and is being rounded down (a continuing problem with representing decimal data with binary numbers).

    Code:
    #include <stdio.h> 
    
    int main()
    {
        float money, remainder; 
        const float PORTIONS[] = {1.0f, 0.25f, 0.1f, 0.05f, 0.01f};
        const char *NAMES[] = {"Dollar", "Quarter", "Dime", "Nickel", "Penny"};
        int i, val;
        
        do{
            printf("Enter an amount of money: $"); 
            scanf("%f",&money); 
        } while(money < 0); 
    
        printf("You've entered: $%.2f\n", money); 
        
        //Divide by .25 to get quarters, remainder / .10 
        //for dimes, remainder by .05 for nickels 
        remainder = money;
        for (i=0; i<5; i++){
            val = (int)(remainder/PORTIONS[i]);
            printf("%8s: %d\n", NAMES[i], val);
            remainder = remainder - (val * PORTIONS[i]);
        }
    
        printf("\n\n");
        for (i=0; i<5; i++){
            printf("%8s: %d\n", NAMES[i], (int)(money/PORTIONS[i])); 
        }
    
        return 0; 
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo