Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-10-2005, 08:35 AM   #1
Member (4 bit)
 
Join Date: Sep 2004
Posts: 13
Figure out my C++ program's problem. YOU CAN FIND THE MISSING PENNY!

I have a program in C++ that lets users enter in the amount of a purchase and the amount they paid. It will then calculate their change in dollars, quarters, dimes, nickels and pennies. Here it is:

PHP Code:

ProgrammerJoseph Malicke 
Date
06-10-05 
Exercise

Version

Problem
Calculate change ($) as long as user wants 
*/ 

#include <iostream> 
#include <conio.h> // for holding DOS window 

using namespace std

// function calculates coins from the change 
void calculateCoins(double theChange); 
// function prints 
void printData(); 

// define variables to hold user input 
double costOfPurchaseamountPaid;     
// holds change 
double change
// hold loop control 
char loopControl 'Y'
// hold change by coins 
int changeDollarchangeQuarterchangeDimechangeNickelchangePenny
     
int main() 

    
// format cout to 2 places 
    
cout.setf(ios::fixed); 
    
cout.setf(ios::showpoint); 
    
cout.precision(2); 
     
    do 
    { 
          
// collect the data 
          
cout << "Please enter the cost of purchase, a space, and the amount paid.\n"
          
cin >> costOfPurchase >> amountPaid
          
// calculate change 
          
change amountPaid costOfPurchase
          
// pass change on to calculate coins 
          
calculateCoins(change); 
          
// now print 
          
printData(); 
          
// ask whether user wants to continue 
          
cout << "Would you like to enter more data?\n"
          
cin >> loopControl
          
cout << endl
    } while (
loopControl == 'Y' || loopControl == 'y'); 
     
    
// Hold DOS window 
    
cout << "\nHit any key to exit:\n"
    
getch(); 
     
    return 
0


void calculateCoins(double theChange

         
// first calculate how many dollars can go into it 
         // we don't care about fractional parts 
         
changeDollar static_cast<int>(theChange 1); 
         
// now subtract those dollars 
         
theChange theChange - (1.00 changeDollar); 
         
// calculate for quarters 
         
changeQuarter static_cast<int>(theChange .25); 
         
// now subtract those quarters 
         
theChange theChange - (.25 changeQuarter); 
         
// calculate for dimes 
         
changeDime static_cast<int>(theChange .10); 
         
// now subtract those dimes 
         
theChange theChange - (.10 changeDime); 
         
// calculate for nickels 
         
changeNickel static_cast<int>(theChange .05); 
         
// now subtract those nickels 
         
theChange theChange - (.05 changeNickel); 
         
// calculate for pennies 
         
changePenny static_cast<int>(theChange .01); 
         
// now subtract those pennies 
         
theChange theChange - (.01 changePenny); 


void printData() 

     
cout << "The cost of purchase was $" << costOfPurchase << " and the paid amount was $" << amountPaid << endl
     
cout << "Your change is $" << change << endl
     
cout << "You should recieve..." << endl
     
cout << changeDollar << " dollars, " << changeQuarter << " quarters, " << changeDime << " dimes, " << changeNickel << " nickels, " << changePenny << " pennies.\n" << endl
      

So, for example, let's say the change is 2.76.

2.75 / 1 = 2.75, casted to an int, drops the decimal
changeDollar = 2
2.75 - (1.00 * 2) = .76

So my algorithm broke down the dollars.

.76 / .25 = 3.04, casted to an int, drops the decimal
changeQuarter = 3
.76 - (.25 * 3) = .1

Now we're down to one penny.

It'll continue to do this until it gets to the pennies and eventually finishes. Now, for all my
test data, the output is logically correct. HOWEVER, when I enter 50.00 for the purchase
amount and 50.01 for the amount paid, the result says this:

Your change is $0.01
You should be paid in...
0 dollars, 0 quarters, 0 dimes, 0 nickels, 0 pennies

It's off by a penny. Why??? It works when I do total 100 and 100.01 paid. In fact, it
even works when I enter 20 and 20.01 paid. I'm almost tempted to write a loop to try
every value up to 500 to see how many are off by a penny.

What's going on? :O

Last edited by aKobrakai; 06-10-2005 at 09:08 AM.
aKobrakai is offline   Reply With Quote
Old 06-12-2005, 05:07 AM   #2
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
C++ floating point is incapable of exactly representing most decimal fractions, regardless of the choice of precision
Use a BCD library( Binary Coded Decimal)

we, engineers have a saying: "precision arithmetic calculations are best done by FORTRAN( computer language/compiler)"
and "dont subtract two number of similar magnitude."
alfie2 is offline   Reply With Quote
Old 06-12-2005, 08:39 PM   #3
Member (11 bit)
 
james8547's Avatar
 
Join Date: Aug 2003
Location: NJ
Posts: 1,099
Even the program we use at work (Portfolio Accounting System) is off a penny some of the time (sometimes even up to 15 cents). We use 6-8 decimal places. We deal with billions of dollars and we usually make an adjusting entry to reconcile it out.

I also had the same problem back then when I was taking an Assembly class.
__________________
P4 2.6C @ 3.12 || ASUS P4C800-E Dlx || Antec SOHO File Server w/ 5 case fans || Antec TruePower 430w PSU || 2 x 512MB Crucial PC3200 DDR || 280GB of total storage 7200rpm ATA100 8mb cache || LiteOn CDRW 52x32x52 || LiteOn DVD+/-RW SOHW-812S || WinXP Pro || Solarism 15" TFT LCD || 500VA TrippLite UPS || Logitech MX-700 Duo || ATI 9800 AIW || Sennheiser HD-555
james8547 is offline   Reply With Quote
Old 06-13-2005, 02:46 PM   #4
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
Quote:
Originally Posted by james8547
Even the program we use at work (Portfolio Accounting System) is off a penny some of the time (sometimes even up to 15 cents). We use 6-8 decimal places. We deal with billions of dollars and we usually make an adjusting entry to reconcile it out.

I also had the same problem back then when I was taking an Assembly class.
My firm uses a "string library" (two's complement) to handle precision, its accurate to 127 decimal places; I hate to think that financial institutions use any thing worse. These "library/objects" can be brought in the open market cheap.

Last edited by alfie2; 06-13-2005 at 02:49 PM.
alfie2 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2