The recently made a change to my code that made it work correctly. I would like someone to help me understand why it works, but it did not work before.
Before (not working correctly)
total = (amountPaid - purchaseAmount) * 100;
totalInt = total //changes from float to int
totalDollars = totalInt / 100;
After (working)
total = amountPaid - purchaseAmount;
totalInt = total * 100; //changes from float to int
totalDollars = totalInt / 100;
My actual program (cash register)
#include <iostream>
using namespace std;
int main ()
{
float purchaseAmount;
float amountPaid;
int pennies;
int nickels;
int dimes;
int quarters;
int dollars;
int totalDollars;
int changeLeftOver;
int changeLeftOver1;
int changeLeftOver2;
int changeLeftOver3;
int totalDimes;
int totalQuarters;
int totalNickels;
int totalPennies;
float total;
int totalInt;
cout <<" How much did your item cost? ";
cin >> purchaseAmount;
cout <<"How much are you paying?" ;
cin >> amountPaid;
total = amountPaid - purchaseAmount;
totalInt = total * 100;
totalDollars = totalInt / 100;
changeLeftOver = totalInt %100;
totalQuarters = changeLeftOver /25;
changeLeftOver1 = changeLeftOver %25;
totalDimes = changeLeftOver1 /10;
changeLeftOver2 = changeLeftOver1 %10;
totalNickels = changeLeftOver2 /5;
totalPennies = changeLeftOver2 %5;
cout << " Your change is " << totalDollars << " dollars " <<endl;
cout << totalQuarters << " quarters " <<endl;
cout << totalDimes << " dimes " <<endl;
cout << totalNickels << " nickels " <<endl;
cout << totalPennies << " pennies " <<endl;
system ("pause");
return 0;
}
//End main