Missing something, works fine until the end
Code:
<iostream>
#include<string>
#include<iomanip>
usingnamespace std;
int main()
{
//declare variables
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
int change = 0;
double amountDue = 0.0;
double amountPaid = 0.0;
double valChange = 0.0;
//enter input items
cout << "Enter the amount due: $";
cin >> amountDue;
cout << "Enter the amount the customer paid: $";
cin >> amountPaid;
//calculate
valChange = amountPaid - amountDue;
change = valChange * 100;
dollars = change / 100;
change = change - (dollars *100);
quarters = change / 25;
change = change - (quarters * 25);
dimes = change / 10;
change = change - (dimes * 10);
nickels = change / 5;
change = change - (nickels * 5);
pennies = change;
//display output
cout << "The change amount is: $" << valChange << endl;
cout << dollars << " dollars " << quarters << " quarters " << dimes << " dimes " << nickels << " nickels " << pennies << " pennies." << endl;
return 0;
}
When I run this, it works fine until nickels and pennies. If there are no nickels, pennies calculate correctly. If there are nickels, it shows 0 pennies always.