Thread: help! I lost 1 cent_C++ beginning

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    help! I lost 1 cent_C++ beginning

    I need to write a program that interacts between cash register and machine. As she inputs total price and payment, the machine will calculate money return in dollars, quaters, dimes, nickles and cents.
    After playing with coding, I regconize I lose 1 cent in some situation. I think converting from "int" -->"double" then return back to "int" in my sourcecode cause this problem, but I can't find the way to fix. Please check my sourcecode and provide me a good solution.
    Code:
    #include <iostream.h>
    #include "textlib.h"
    #include <iomanip.h>
    
    int main()
    {
    
    	//Variable declaration
    	double payment, totalprice, exchange;
    	int dollars, quarters, dimes, nickles, pennies, coinChange;
    
    	// Input from cash register
    	cout << "Enter total price and your payment: ";
    	cin >> totalprice >> payment;
    
    	//Calucate the exchange
    	exchange = payment - totalprice;
    
    	//Determine the money return in dollars, quaters, nickels and pennies 
    	dollars = int(exchange);
    
    	coinChange =int((exchange - dollars) *100);
    	
    	quarters = coinChange / 25;
    	coinChange %= 25;
    	
    	dimes = coinChange / 10;
    	coinChange %= 10;
    	
    	nickles = coinChange / 5;
    	
                    pennies = coinChange % 5;
    
    
    	//Output detail transaction
    	
    	cout << "Purchase total:"<< setreal (20,2) << totalprice << endl;
    	cout << "Payment       :"<< setreal (20,2) << payment << endl;
    	cout << "Change        :"<< setreal (20,2) << exchange << endl;
    	cout << "Dollars       :"<< setw(20) << dollars << endl;
    	cout << "Quarters      :"<< setw(20) << quarters << endl;
    	cout << "Dimes         :"<< setw(20) << dimes << endl;
    	cout << "Nickles       :"<< setw(20) << nickles << endl;
    	cout << "Pennies       :"<< setw(20) << pennies << endl;
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Other than not being familiar with the setreal manipulator you use to format the output, I don't see a problem with your code. The way I'd try to track down the problem is to insert cout statements after each value is determined in addition to at the end (ya, I know it's overkill, but sometimes I'll pick up something that way). Then I'd run a number of trials keeping track of expected output and observed output looking for a pattern. Then I'd evaluate the cause for the pattern. Maybe it's a rounding error someplace, maybe it's a bug in your compiler. If you can narrow it down, that might help someone help you figure it out.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    Other than not being familiar with the setreal manipulator you use to format the output, I don't see a problem with your code
    setreal(), which takes 2 arguments "w" (width) and "p" (position), works similarly to setw() but much more power by defining how many significants after ".". For example, the input is real number 8.3243242, then setreal(20,2) will output 8.32 and setw(20).
    I tracked down the value "coinChange" like yours. As I said "I think converting from "int" -->"double" then return back to "int" in my sourcecode cause this problem", precisely at this line
    Code:
    coinChange =int((exchange - dollars) *100);
    I lost 1 cent.

    Note: I tested with 2 possible ways in Visual C++ 5.0 and 6.0. I enterd total price and payment 3.08 and 10. The program works fine! but with 3.08 and 9.99 I lost 1 cent.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Floating point numbers can't represent all whole numbers precisely. One possible solution is to add .5 to your calculation before rounding:
    Code:
    	coinChange =int((exchange - dollars) *100 + 0.5);
    Or another idea is instead of using doubles, read in the amounts as strings, then separate each into dollars and cents and store them as ints.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I remember this problem... yeah, using floats will lose you a penny here and there... it shouldn't happen all the time though... if your constantly over/under a penny, then something's wrong, but if it's just intermittent, don't worry about your code... this is how I would solve it:
    Code:
    int Amount;
    float Currency;
    
    //take in currency
    std::cin>>Currency;
    
    //to help float innacuracy
    amount=static_cast<int>(Currency*100);
    
    //do math/other stuff here (don't forget your working in pennies now)
    
    //then for output
    currency=static_cast<float>(currency)/100.0;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    IEEE floats are (basically) constructed by summing up a series of up to 23 times 1/2^n. If 1/2^n fits into the number, bit number n is set. As you can see, numbers like 0.50, 0.75, 0.25 ... have an exact binnary representation (10000...(18 more "0") and 11000...(18 more...) and 01000 (18 you figure it...)), but a lot of other numbers don't have it (like an ordinary 0.8).

    I'd suggest you read in a string and strip the dot before converting it into integer. That way you don't have to work with floats at all. Of course you are working with a value 100 times that of the user input, so you'd have to take care of that in your calculations. You will also need to make sure that the user won't enter something like 123.456 (3 numbers after dot)
    [code]

    your code here....

    [/code]

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I agree, store it as an integer number of pennies and use integer math, it makes the most sense.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. Replies: 15
    Last Post: 05-13-2006, 09:28 PM
  3. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM