1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Before you get too tangled up in this code, consider using some kind of array-type container instead of these completely unrelated variables. For example, define values in cents as `{1, 5, 10, 25, 100, 500, 1000, 2000, 5000}` and then have the corresponding counts in an array with similar indexes. The text "nickels" means nothing to C++, but the value `5` can. – tadman Feb 15 '19 at 18:18
  • 6
    Also, money should be handled *without* floats or doubles. Track it in units of cents (0.01). Floating point values are *approximations*, not exact values. See [this](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). – wallyk Feb 15 '19 at 18:22
  • *i commented out the cout/cin statements at the beginning so everytime i tested the program i didnt have to enter in the number* The right thing to do and something many people do not. I salute. – user4581301 Feb 15 '19 at 18:27
  • Just to re-iterate. It's far simpler to use an integer for the number of pennies than to use floating point representations and be surprised when rounding or representation limits introduce an inaccuracy – Tim Randall Feb 15 '19 at 18:33
  • When you will use array from the first comment, the solution will become simple: you just iterate from greater values to smaller ones, and figure out how many of such values fits in your remaining change. –  Feb 15 '19 at 23:36

1 Answers1

1

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

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770