Reputation:
I'm new to computer science, and am currently learning C++.
We were given an assignment to calculate change with large quantities. Say, for example, my change at the grocery store was $37.37. The output of the program would tell me how much of each bill in change I'd get (1 twenty dollar bill, 1 ten dollar bill, 1 five dollar bill, 2 one dollar bills, one quarter, one dime, and two pennies).
I've already figured out how to do it with coins from a previous assignment, but the fact that these are whole dollars now has caused me to hit a wall.
I've tried dividing by the bill denomination, but I can't figure that out (I commented out the cout
/cin
statements at the beginning so every time I test the program I don't have to enter in the number):
#include <iostream>
using namespace std;
int main() {
double price, change;
int paymentQ, quarters, dimes, nickels, pennies,
twentyDollar, fiftyDollar,
fiveDollar, dollars, tenDollar, totalChange;
//cout << "What is price? " << endl;
//cin >> price
//cout << "Please insert cash or select payment type: " << endl;
//cin >> paymentQ;
//change = (paymentQ - price);
change = 37.37;
dollars = static_cast<int>(change);
fiveDollar =
tenDollar =
twentyDollar =
fiftyDollar =
{
dollars = static_cast<int>(change);
quarters = (((change - dollars) * 100) / 25);
dimes = (((change - dollars) * 100) - (quarters * 25)) / 10;
nickels = (((change - dollars) * 100) - (quarters * 25) - (dimes * 10)) / 5;
pennies = (((change - dollars) * 100) - (quarters * 25) - (dimes * 10) - (nickels * 5) + .5);
}
cout << "\nYour change is\n " << twentyDollar << " Twenty Dollar bill/s " << endl
<< tenDollar << " Ten Dollar Bill/s" << endl <<
fiveDollar << " Five Dollar Bill/s" << endl <<
dollars << " One Dollar Bill/s" << endl <<
quarters<< " Quarters "<< endl <<
dimes<< " dimes " << endl <<
nickels << " Nickels" << endl <<
pennies << " pennies "<< endl << endl;
return 0;
}
Upvotes: 1
Views: 526
Reputation: 595837
The way you are assigning your denomination variables is all wrong, and probably shouldn't even compile. Even if it does, it has a lot of repeatedness in it and is certainly not easy to read in general.
Try something more like this instead:
#include <iostream>
using namespace std;
int main() {
double price, paymentQ, change;
int quarters, dimes, nickels, pennies,
fiftyDollars, twentyDollars, tenDollars, fiveDollars, oneDollars;
//cout << "What is price? " << endl;
//cin >> price
//cout << "Please insert cash or select payment type: " << endl;
//cin >> paymentQ;
//change = (paymentQ - price);
change = 37.37;
// despite what cout shows, 37.37 is actually 37.369999999999997 and
// so would become 3736 instead of 3737 when multiplied by 100 and
// truncated as-is to an int, so it needs to be rounded up a little
// bit to account for that (see https://stackoverflow.com/questions/149033/) ...
pennies = (change * 100) + 0.1; // see https://ideone.com/wmlzlm for why this work...
fiftyDollars = pennies / 5000; pennies %= 5000;
twentyDollars = pennies / 2000; pennies %= 2000;
tenDollars = pennies / 1000; pennies %= 1000;
fiveDollars = pennies / 500; pennies %= 500;
oneDollars = pennies / 100; pennies %= 100;
quarters = pennies / 25; pennies %= 25;
dimes = pennies / 10; pennies %= 10;
nickels = pennies / 5; pennies %= 5;
cout << "\nYour change is " << change << "\n"
<< twentyDollars << " Twenty Dollar Bill/s\n"
<< tenDollars << " Ten Dollar Bill/s\n"
<< fiveDollars << " Five Dollar Bill/s\n"
<< oneDollars << " One Dollar Bill/s\n"
<< quarters << " Quarter/s\n"
<< dimes << " Dime/s\n"
<< nickels << " Nickel/s\n"
<< pennies << " Penny/s\n"
<< endl;
return 0;
}
Output:
Your change is 37.37 1 Twenty Dollar Bill/s 1 Ten Dollar Bill/s 1 Five Dollar Bill/s 2 One Dollar Bill/s 1 Quarter/s 1 Dime/s 0 Nickel/s 2 Penny/s
Upvotes: 1