Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- int makeChange(float total, float paid)
- {
- float denom[] = {.01, .05, .10, .25, 1, 5, 10, 20};
- string denomStr[2][8] = {
- { "penny", "nickel", "dime", "quarter", "single", "five", "ten", "twenty" },
- { "pennies", "nickels", "dimes", "quarters", "singles", "fives", "tens", "twenties" },
- };
- float change = paid - total;
- cout.precision(2);
- if (change < 0)
- {
- cout << "You don't have enough to purchase this item." << endl;
- return -1;
- }
- else if (change == 0)
- {
- cout << "No change given." << endl;
- return 0;
- }
- else
- cout << fixed << total << " from " << paid << " returns:" << endl;
- // main algorithm:
- int d, i = 7;
- while (i >= 0)
- {
- if (change >= denom[i])
- {
- d = 0;
- while (change >= denom[i])
- {
- change = change - denom[i];
- d++;
- }
- if (d == 1)
- cout << d << " x " << denomStr[0][i] << endl;
- else
- cout << d << " x " << denomStr[1][i] << endl; //plurals
- }
- i--;
- }
- return 0;
- }
- int main()
- {
- makeChange(3.21, 5.00);
- makeChange(5.00, 3.21);
- makeChange(19.74, 20.00);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement