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;
}