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;
}