Help?

This is a discussion on Help? within the C++ Programming forums, part of the General Programming Boards category; I can't understand why it is not returning the right amount of pennies in this program: for instance, if I ...

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Help?

    I can't understand why it is not returning the right amount of pennies in this program:
    for instance, if I enter item amount is 12.00 and amount tendered is 99.99, it should say number of pennies (for change given back) is 4, not 3! On some instances it works correctly, but on others like this it will not.
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    class Change
    {
          public:
                 void setEachToZero();
                 double AmountToMakeChangeFor(double amt_p);
                 void showEach();
                 
          private:
                  int numTwenties;
                  int numTens;
                  int numFives;
                  int numOnes;
                  int numQuarters;
                  int numDimes;
                  int numNickels;
                  int numPennies;
                  
    };
    
    int main()
    {
        double amount; 
        double tender; 
        cout << "Please enter the amount of the item: "; 
        cin >> amount; 
        cout << endl; 
        cout << "Please enter the amount tendered: "; 
        cin >> tender; 
        cout << endl; 
        Change amt_p;
        double change2 = tender - amount;
        
        
        
        
        if (change2 > 0)
        {
            amt_p.setEachToZero();
            amt_p.AmountToMakeChangeFor(change2);
    	amt_p.showEach();
        }
        else if (change2 < 0)
        {
             cout << "You owe an additional amount of " << amount - tender << endl;
        }           
        else
        {
            cout << "You broke even." << endl;
        }
        
    
        system("pause");
        return 0;
    }
    
    void Change::showEach() 
    {
            cout << "Twenties:    \t" << numTwenties << endl; 
            cout << "Tens:          \t" << numTens << endl;
            cout << "Fives:         \t" << numFives << endl; 
        	cout << "Ones:        \t" << numOnes << endl; 
            cout << "Quarters:   \t" << numQuarters << endl; 
            cout << "Dimes:       \t" << numDimes << endl; 
            cout << "Nickels:     \t" << numNickels << endl; 
            cout << "Pennies:    \t" << numPennies << endl; 
         
    }
    
    void Change::setEachToZero()
    {
         numTwenties = 0;
         numTens = 0;
         numFives = 0;
         numOnes = 0;
         numQuarters = 0;
         numDimes = 0;
         numNickels = 0;
         numPennies = 0;
    }
    
    double Change::AmountToMakeChangeFor(double amt_p)
    {
         numTwenties = (amt_p / 20);
         amt_p = (amt_p - (numTwenties * 20));
         numTens = (amt_p / 10);
         amt_p = (amt_p - (numTens * 10));
         numFives = (amt_p / 5);
         amt_p = (amt_p - (numFives * 5));
         numOnes = (amt_p /1);
         amt_p = (amt_p - (numOnes * 1));
         numQuarters = (amt_p / .25);
         amt_p = (amt_p - (numQuarters * .25));
         numDimes = (amt_p / .10);
         amt_p = (amt_p - (numDimes * .10));
         numNickels = (amt_p / .05);
         amt_p = (amt_p - (numNickels * .05));
         numPennies = (amt_p / .01);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I didn't look closely, but it's possible that it's round-off errors.

    You could multiply your amounts by 100 and use integers everywhere, which would help avoid that.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21