I have tried in vain to debug this. The logic seems to fail around dimes. For most of the values I put in, the logic works fine. However sometimes, there will be a "penny" left in difference and other times there will be a negative number. Any ideas how this happens?
Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
// self documenting code begin
cout << "This program calculates the amount of change\ngiven back after a purchase";
// self documening code end
cout << "\nEnter the purchase amount: ";
double purchase;
cin >> purchase;
cout << "\nEnter the amount of money given: ";
double money;
cin >> money;
double difference = money - purchase;
double change = difference;
int dollar = 1;
double halfDollar = .5;
double quarter = .25;
double dime = .1;
double nickel = .05;
double penny = .01;
for (int dollars = 0; difference >= 1; dollars++)
{
cout << difference <<" DEBUGGING LOGIC - subtracting a dollar\n"; // for debugging
difference = difference - dollar;
cout << difference <<" DEBUGGING LOGIC - I subtracted a dollar\n"; // for debugging
}
for (int halfDollars = 0; difference >= 0.50; halfDollars++)
{
cout << difference <<" DEBUGGING LOGIC - subtracting a half dollar\n"; // for debugging
difference = difference - halfDollar;
cout << difference <<" DEBUGGING LOGIC - I subtracted a half dollar\n"; // for debugging
}
for (int quarters = 0; difference >= 0.25; quarters++)
{
cout << difference <<" DEBUGGING LOGIC - subtracting a quarter\n"; // for debugging
difference = difference - quarter;
cout << difference <<" DEBUGGING LOGIC - I subtracted a quarter\n"; // for debugging
}
for (int dimes = 0; difference >= 0.10; dimes++)
{
cout << difference <<" DEBUGGING LOGIC! - subtracting a dime\n"; // for debugging
difference = difference - dime;
cout << difference <<" DEBUGGING LOGIC - I subtracted a dime\n"; // for debugging
}
for (int nickels = 0; difference >= 0.05; nickels++)
{
cout << difference <<" DEBUGGING LOGIC - subtracting a nickel\n"; // for debugging
difference = difference - nickel;
cout << difference <<" DEBUGGING LOGIC - I subtracted a nickel\n"; // for debugging
}
for (int pennies = 0; difference > 0.01; pennies++)
{
cout << difference <<" DEBUGGING LOGIC! - subtracting a penny\n"; // for debugging
difference = difference - penny;
cout << difference <<" DEBUGGING LOGIC - I subtracted a penny\n"; // for debugging
}
if (difference > 0.01)
{
cout << difference <<" DEBUGGING LOGIC! - Added a penny to make it right?\n"; // for debugging
++pennies;
}
if (difference < 0)
{
cout << difference <<" DEBUGGING LOGIC! - Subtracted a penny to make it right?\n"; // for debugging
--pennies;
}
// Self documenting output to screen
cout << difference <<" THE END RESULT OF DIFFERENCE DEBUGGING LOGIC!\n"; // for debugging
cout << "\nPlease give the customer " << change << " back in change\n";
cout << dollars << " dollars\n";
cout << halfDollars << " half dollars\n";
cout << quarters << " quarters\n";
cout << dimes << " dimes\n";
cout << nickels << " nickels\n";
cout << pennies << " pennies\n";
cin.ignore();
return 0;
}