Thread: Help figuring out why calculations are not working correctly

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    Help figuring out why calculations are not working correctly

    Write a program that will calculate the types of coins that will add up to a Dollar cents number that is input by the user. For example if the user inputs 1.50, the program should output “6 quarters are equal to $1.50”. Take into account, quarters, dimes, nickels and pennies. As another example if the user inputs 2.12, the program should ouput “8 quarters, 1 dime and 2 pennies are equal to $2.12”.

    Your program should give the minimum number of coins that will add up to the total amount, i.e. Your output for $1.50 should be 6 quarters and not 15 dimes or 30 nickels.

    for some reason when i input certain values like .95 it doesn't set dimes to 2 but sets it to 1 instead. i can't figure out it not working correctly. PLZ help
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    	double money,money2;
    	int quarter=0,dime=0,nickle=0,cent=0;
    	printf("Input the ammount of money: ");
    	scanf("%lf",&money);
    
    	money2=money;
    	printf("%lf \n",money2);
    	if(money2/.25>=1)
    	{
    		money2=money2/.25;
    		quarter=(int) money2;
    		money2=money-quarter*.25;
    		printf("%lf \n",money2);
    
    	}
    	if(money2/.1>=1)
    	{
    		money2=money2/.1;
    		dime= money2;//******************
    		money2=money-quarter*.25-dime*.1;
    		printf("%lf \n",money2);
    	}
    	if(money2/.05>=1)
    	{
    		money2=money2/.05;
    		nickle=(int)money2;
    		money2=money-quarter*.25-dime*.1-nickle*.05;
    		printf("%lf \n",money2);
    	}
    	if(money2/.01>=1)
    	{
    		money2=money2/.01;
    		cent=(int)money2;
    		money2=money-quarter*.25-dime*.1-nickle*.05-cent*.01;
    		printf("%lf \n",money2);
    	}
    
    
    
    
    	printf("%d quarters\n",quarter);
    	printf("%d dimes\n",dime);
    	printf("%d nickles\n",nickle);
    	printf("%d cents\n",cent);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you deal with doubles and floats you get into the approximation problem. First thing I'd do is change >= 1 to >=.999. That should handle the approximation problem.

    Try that real quickly, and see what that does.

    Another way to do this is to round the double by adding 0.5 and then dividing by 1, and casting that to an int variable, before the comparison.

    The way I like to deal with money is to immediately convert all monies to pennies, and then handle all calculations using integers. Final print out compensates for that conversion.
    Last edited by Adak; 12-09-2010 at 03:10 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Rounding errors. There are certain values that a floating point type cannot represent exactly. 0.1 is one of them. You also need to account for whether rounding goes up or down.

    You'd be better off converting values to integers and doing all your working with integer arithmetic (e.g. $1.50 is 150 cents, a quarter is 25 cents rather than $0.25). Mathematically the same, but not subject to things you don't understand about floating point.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    didn't help at all. still have the same problem. here is a bad output. look at the cents.

    Input the ammount of money: 1.49
    1.490000
    0.240000
    0.040000
    0.010000
    5 quarters
    2 dimes
    0 nickles
    3 cents
    Press any key to continue . . .

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't like your logic. You have a lot of division by a float, and then there's casting to an int, done too late (imo).

    It's hodgey-podgey stuff. Clean it up, and simplify it. Convert to integer ONE time, right at the start, and never do it again.

    The problem is, you're close - and so you want to keep going and get that last %1 correct. Close is the greatest enemy of right. If you were WAY wrong, you'd change your code in a second. But you're close, so...

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
      double money;
      int money2;
    
      printf("Input the ammount of money: ");
      scanf("%lf",&money);
    
      money2= (int)( money * 100);
      printf("%d cents\n\n",money2);
    
      printf("%d quarters\n",money2 / 25);
      money2 %= 25;
      printf("%d dimes\n",money2 / 10);
      money2 %= 10;
      printf("%d nickles\n",money2 / 5);
      money2 %= 5;
      printf("%d cents\n",money2);
    }
    Last edited by CommonTater; 12-09-2010 at 04:03 PM.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Thank You very much

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by koolbklyn View Post
    Thank You very much
    LOL...Sometimes I just feel like showing off....

    However; I will urge you to take the time to understand how it works.

    That's a class assignment, your teacher will find it. So don't be handing it in.
    Last edited by CommonTater; 12-09-2010 at 04:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2009, 08:29 PM
  2. Help explaining test questions
    By Sentral in forum General Discussions
    Replies: 26
    Last Post: 11-09-2009, 11:10 PM
  3. Program stopping working
    By SterlingM in forum C++ Programming
    Replies: 24
    Last Post: 10-17-2009, 02:38 PM
  4. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM