Oct 1, 2014 at 2:23am UTC
I'm new to C++ and am having trouble with programming the output of the change after finding the differences between the price of the item and how much the user is going to pay. Here is the code I have so far. It works when the change is in 100's and 50's but after that it doesn't seem to work and I'm not sure where to go from here. I know there is a lot of extra calculating on here but I'd also like to keep it as simple as possible. Thank you!
#include <iostream>
using namespace std;
int main()
{
//enter variables
float price;
cout << "Enter price: " << endl;
cin >> price;
float pay;
cout << "Enter pay: " << endl;
cin >> pay;
float change;
change = (pay - price)*100;
cout << "Change due: " << change/100 << endl;
//values of the money
int onehundred = 100.00;
int fifty = 50.00;
int twenty = 20.00;
int ten = 10.00;
int five = 5.00;
int one = 1.00;
int half = 0.50;
int quarters = 0.25;
int dimes = 0.10;
int nickels = 0.05;
int pennies = 0.01;
//calculating how many you need
if (change >= 10000)
{
onehundred = change / 10000;
}
if (change >= 5000)
{
fifty = (change - (onehundred * 10000)) / 5000;
}
if (change >= 2000)
{
twenty = (change - (onehundred * 10000) - (fifty * 5000)) / 2000;
}
if (change >= 1000)
{
ten = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000)) / 1000;
}
if (change >= 500)
{
five = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000)) / 500;
}
if (change >= 100)
{
one = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500)) / 100;
}
if (change >= 0.5000)
{
half = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500) - (one * 100)) / 5000;
}
if (change >= 0.2500)
{
quarters = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500) - (one * 100) - (half * 5000)) / 2500;
}
if (change >= 0.1000)
{
dimes = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500) - (one * 100) - (half * 5000) - (quarters * 2500)) / 1000;
}
if (change >= 0.0500)
{
nickels = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500) - (one * 100) - (half * 5000) - (quarters * 2500) - (dimes * 1000)) / 500;
}
if (change >= 0.0100)
{
pennies = (change - (onehundred * 10000) - (fifty * 5000) - (twenty * 2000) - (ten * 1000) - (five * 500) - (one * 100) - (half * 5000) - (quarters * 2500) - (dimes * 1000) - (nickels * 500)) / 100;
}
//the final product
cout << "You should give the customer this change: " << endl;
cout << "$100 dollar bills: " << onehundred << endl;
cout << "$50 dollar bills: " << fifty << endl;
cout << "$20 dollar bills: " << twenty << endl;
cout << "$10 dollar bills: " << ten << endl;
cout << "$5 dollar bills: " << five << endl;
cout << "$1 dollar bills: " << one << endl;
cout << "Half dollars: " << half << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickels << endl;
cout << "Pennies: " << pennies << endl;
return 0;
}
Oct 1, 2014 at 6:04am UTC
Stop.
Just look it up, this is like one of the most highest homework question asked here, just look it up.