Thread: a dime off.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    a dime off.

    Hey guys-
    I am a dime off on this program and I can't figure out why. Any suggestions? Thanks.

    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
        int dollars,
            quarters,
            dimes;
    };
    
    
    int main(void)
    {
        dollarsQuartersDimes dQD1, sum, dQD2 = {  };
        
        printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
        
        scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
        
        printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
        
        scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
        
        sum.dollars = dQD1.dollars + dQD2.dollars;  
        sum.quarters = dQD1.quarters + dQD2.quarters;
        sum.dimes = dQD1.dimes + dQD2.dimes;
        
        while (sum.quarters > 3)
            {
            ++sum.dollars;
            sum.quarters = sum.quarters - 4;    
            }
        
        while (sum.dimes > 9)
            {
            ++sum.dollars;
            sum.dimes = sum.dimes - 10;    
            }
            
        while (sum.dimes > 2)
            {
            ++sum.quarters;
            sum.dimes = sum.dimes - 2.5;    
            }
        
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
                
    return 0;
    Attached Images Attached Images a dime off.-ass8error-png 

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your last loop (mapping dimes to quarters) is the culprit.
    Code:
        sum.dimes = sum.dimes - 2.5;
    will do the subtraction in floating point, and then round toward zero in storing the result to sum.dimes. So, if sum.dimes is positive, this line will subtract 3.

    You would be better off converting the input into dollars and cents, and then convert the number of cents back to quarters and dimes. (That's one way, anyway). Otherwise you're being affected by the fact that a quarter is not a whole number of dimes.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Like this? But it still does not give me correct change.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
    	while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    	
    	while (sum.dimes > 90)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.dimes = sum.dimes - 100;	
    	    }
    		
    	while (sum.dimes > 20)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    	
    	    sum.dollars = sum.dollars / 100;
    	    sum.quarters = sum.quarters / 100;
    	    sum.dimes = sum.dimes / 100;
    	    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    can anyone help? thanks.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your sum of <coin> is really just pennies now. Change your while loops, accordingly. After you run through the quarters in the while loop, you can't say sum quarters += 25. That's for pennies, and you have already changed pennies back into quarters: so you need sum quarters++;

    Same for all the rest of the loops. If you're lost with this, grab some coins with a bit of each type, and work it through by hand AS IF you were the program. Talk to yourself, about what you're doing, as you go through it. The light bulb will come on shortly.

    Then look at your while(loopTest), loopTest. Those aren't quite right, are they? If I have 21 cents, I don't have a quarter, see?
    Last edited by Adak; 10-22-2012 at 07:59 PM.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by Adak View Post
    If I have 21 cents, I don't have a quarter, see?
    wouldn't it be impossible to have 21 cents? so, if it's above 20 cents, it has to be 30 cents, right?

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ok. here's my changed code.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
    	while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    	
    	while (sum.dimes > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.dimes = sum.dimes - 100;	
    	    }
    		
    	while (sum.dimes > 24)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    	
    	    sum.dollars = sum.dollars / 100;
    	    sum.quarters = sum.quarters / 25;
    	    sum.dimes = sum.dimes / 10;
    					    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }
    why am i getting 6 quarters? there should never be more than 3 quarters.
    a dime off.-ass8error-png

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I agree with grumpy:
    Quote Originally Posted by grumpy
    You would be better off converting the input into dollars and cents, and then convert the number of cents back to quarters and dimes.
    Create a variable named cents. Compute and store the total number of cents in the quarters and dimes into this cents variable. Then, compute the maximum number of dollars from this total number of cents, adding that figure to the total number of dollars and subtracting 100 times of that from the total number of cents. After that, compute the maximum number of quarters from the remaining total number of cents such that you are left with a whole number of dimes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why are you using subtraction instead of division? Even if it's not perfect.

    <num quarters> / <4 quarters> = <num dollars>

    <num dimes> * 10 / <25 cents> = <num quarters> Scaling by a factor of ten eliminates the need to do floating point calculations.

    I'm not sure I get it. I mean, every denomination of dollars is important. Even pennies, which everybody hates.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    Create a variable named cents..
    my assignment was to use just the 3 variables i have already used.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    Even pennies, which everybody hates.
    i am trying to change everything to pennies, and then change it back.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rosemary
    my assignment was to use just the 3 variables i have already used.
    Does the requirements really say that you are not to use any other variables, or does it say that you must use this struct to store the input and final result?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    it says "use 3 simple variables".
    but, i think i got it.
    i should have been starting with the smallest coin instead.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
        
    	while (sum.dimes > 24)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    
    
        while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    		
    	sum.dollars = sum.dollars / 100;
    	sum.quarters = sum.quarters / 25;
    	sum.dimes = sum.dimes / 10;
    					    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    thanks for the help everybody

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: If you only have three denominations dollars, quarters, and dimes.
    The total value will end with either 5 or 0; if 5 it means you have an old number of quarters.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed