0

Can someone please tell me where I'm wrong here? I'm trying to make a calculator to find correct change when given a double. I haven't added anything to limit it to 2 decimal places, but that's not the issue. I'm newish to coding, and know it could probably be changed for brevity, but purely functionally the calculator is correct every time, unless the number ends in 8 or 9, then the pennies are 1 less than they should be (ie 153.37 will tell me 1 quarter, 1 dime and 2 pennies, but 153.39 will tell me 1 quarter, 1 dime and 3 pennies). I am confused.

#include <iostream>
using namespace std;

void ComputeChange(double totVal, int& numHundreds, int& numFifties, int& numTwenties, 
    int& numTens, int& numFives, int& numOnes, int& numQuarters, int& numDimes, int& 
    numNickels, int& numPennies) {
    double newVal;
    cout << "Your change:" << endl;
   
    numHundreds = totVal / 100;
    totVal -= (numHundreds * 100);
   
    numFifties = totVal / 50;
    totVal -= (numFifties * 50);
   
    numTwenties = totVal / 20;
    totVal -= (numTwenties * 20);
   
    numTens = totVal / 10;
    totVal -= (numTens * 10);
   
    numFives = totVal / 5;
    totVal -= (numFives * 5);
   
    numOnes = totVal / 1;
    totVal -= (numOnes * 1);
   
    newVal = (totVal * 100);
    numQuarters = newVal / 25;
    newVal -= (numQuarters * 25);
   
    numDimes = newVal / 10;
    newVal -= (numDimes * 10);
   
    numNickels = newVal / 5;
    newVal -= (numNickels * 5);
   
    numPennies = newVal / 1;
}

int main() {
   
    double userVal;
    int numHundreds;
    int numFifties;
    int numTwenties;
    int numTens;
    int numFives;
    int numOnes;
    int numQuarters;
    int numDimes;
    int numNickels;
    int numPennies;

    cout << "Enter total amount: " << endl;
    cin >> userVal;

    cout << userVal << endl;

    ComputeChange(userVal, numHundreds, numFifties, numTwenties, numTens, numFives, numOnes, 
    numQuarters, numDimes, numNickels, numPennies);

    cout << "Hundreds: " << numHundreds << endl;
    cout << "Fifties: " << numFifties << endl;
    cout << "Twenties: " << numTwenties << endl;
    cout << "Tens: " << numTens << endl;
    cout << "Fives: " << numFives << endl;
    cout << "Ones: " << numOnes << endl;
    cout << "Quarters: " << numQuarters << endl;
    cout << "Dimes: " << numDimes << endl;
    cout << "Nickels: " << numNickels << endl;
    cout << "Pennies: " << numPennies << endl;

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Don't use floating-point types for money. Use an integer with the number of pennies instead. Floating point arithmetic will lead to rounding errors. – Some programmer dude Sep 23 '22 at 05:32
  • 2
    Please read [is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). In other words, this: `numHundreds = totVal / 100;` has broken your program. – PaulMcKenzie Sep 23 '22 at 05:32
  • 1
    Don't perform exact currency calculations with `double`. You will suffer from floating point error. Convert the total amount to cents first: `int cents = std::round(totVal * 100);` and then do all your operations in cents. _e.g._ `numHundreds = cents / 10000; cents %= 10000;` _etc_. – paddy Sep 23 '22 at 05:33
  • Style note: aside from indentation, operator precedence means you can gett rid of a lot of parentheses. `newVal -= (numNickels * 5);` is equivalent to `newVal -= numNickels * 5;` – Chris Sep 23 '22 at 05:35
  • *unless the number ends in 8 or 9, then the pennies are 1 less than they should be* -- Which is why financial companies in many cases prohibit their applications from using floating point variables. Multiply that one penny difference by a thousand, or a million. – PaulMcKenzie Sep 23 '22 at 05:38
  • 1
    Someone should write an office comedy movie where that's a central plot point. Oh... – Chris Sep 23 '22 at 05:41
  • As a study exercise for you, here is a way to write this program more cleanly: https://godbolt.org/z/796zrszGW – paddy Sep 23 '22 at 05:59

0 Answers0