4
\$\begingroup\$

I am currently in an intro C++ programming class. We have an assignment where we have to convert a dollar and cents amount, say $192.89, to bills and coins using the least amount of both. Below is the program I have written.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    double TotalAmount;
    int DollarsConversion;
    int Change;     
    int Dollars;     
    int Hundreds = 0;
    int Fifties = 0;
    int Twenties = 0;
    int Tens = 0;
    int Fives = 0;
    int Ones = 0;
    int CentsConversion;    
    int Cents;
    int Quarters = 0;
    int Dimes = 0;
    int Nickels = 0;
    int Pennies = 0;

    cout << "Please enter the amount to convert: $";
    cin >> TotalAmount;

    //Dollars Calculation.
    DollarsConversion = TotalAmount * 100;
    Hundreds = DollarsConversion / 10000;
    Change = DollarsConversion % 10000;
    Fifties = Change / 5000;
    Change %= 5000;
    Twenties = Change / 2000;
    Change %= 2000;
    Tens = Change / 1000;
    Change %= 1000;
    Fives = Change / 500;
    Change %= 500;
    Ones = Change / 100;
    Change %= 100;

    //Resets the Stack to calculate for the cents.
    Dollars = TotalAmount;   
    CentsConversion = TotalAmount * 1000;   
    Dollars *= 1000;    
    Cents = CentsConversion - Dollars;

    //Cents Calculation.
    Quarters = Cents / 250;
    Change = Cents % 250;
    Dimes = Change / 100;
    Change %= 100;
    Nickels = Change / 50;
    Change %= 50;
    Pennies = Change / 10;
    Change %= 10;

    cout << "\nNumber of Hundred Dollar Bills: " << Hundreds << endl;
    cout << "Number of Fifty Dollar Bills: " << Fifties << endl;
    cout << "Number of Twenty Dollar Bills: " << Twenties << endl;
    cout << "Number of Ten Dollar Bills: " << Tens << endl;
    cout << "Number of Five Dollar Bills: " << Fives << endl;
    cout << "Number of One Dollar Bills: " << Ones << endl;

    cout << "\nNumber of Quarters: " << Quarters << endl;
    cout << "Number of Dimes: " << Dimes << endl;
    cout << "Number of Nickels: " << Nickels << endl;
    cout << "Number of Pennies: " << Pennies << endl;

    return 0;
}
\$\endgroup\$
  • \$\begingroup\$ double's do not hold a value exactly. Convert your number to an integer (the number of cents). Then use that to do your calculations. \$\endgroup\$ – Martin York Apr 21 '15 at 0:00
  • \$\begingroup\$ Don't forget to use the % operator. It will simplify your code considerably. \$\endgroup\$ – Martin York Apr 21 '15 at 0:01
  • \$\begingroup\$ Sorry, The identifier Pennies was initialized to int, not float in my program. Finally got my code to work properly. It was truncating the integer numbers CentConversion and Dollar. I managed a work around to cut the divisor to 1000, instead of 10000. Then removed the division of the Cents formula and just had them subtract from each other. Then changed the quotients of each of the change identifiers to be 10x what they were. All seems to work pproperly now! Thanks for your help I have edited the code to reflect this so that if anyone else is having this issue can refer to my corrected coding. \$\endgroup\$ – Alex Apr 21 '15 at 2:34
  • \$\begingroup\$ Glad you got it working. A fudge I have often seen involves the use of floor() or ceil() - you can guess yourselves how. \$\endgroup\$ – mckenzm Apr 21 '15 at 3:51
1
\$\begingroup\$

You have a lot of copy-and-paste code, where the only things that vary are the variable name, denomination, and textual description. You can generalize the logic.

#include <iostream>

struct Denomination {
    const int cents;
    const char *description;
};

const Denomination DENOMINATIONS[] = {
    { 10000, "Hundred-Dollar Bills" },
    {  5000, "Fifty-Dollar Bills" },
    {  2000, "Twenty-Dollar Bills" },
    {  1000, "Ten-Dollar Bills" },
    {   500, "Five-Dollar Bills" },
    {   100, "One-Dollar Bills" },
    {    25, "Quarters" },
    {    10, "Dimes" },
    {     5, "Nickels" },
    {     1, "Pennies" }
};

int main() {
    double totalAmount;
    std::cout << "Please enter the amount to convert: $";
    std::cin >> totalAmount;
    std::cout << '\n';

    int cents = 100 * totalAmount;
    for (int i = 0; i < sizeof(DENOMINATIONS) / sizeof(DENOMINATIONS[0]); ++i) {
        std::cout << "Number of " << DENOMINATIONS[i].description
                  << ": " << (cents / DENOMINATIONS[i].cents) << '\n';
        cents %= DENOMINATIONS[i].cents;
    }
}

Also note:

  • A somewhat common convention is to use this NamingConvention for types, this namingConvention for variable names, and this NAMING_CONVENTION for constants.
  • Your DollarsConversion variable is misnamed, in my opinion, as it actually stores the number of cents.
\$\endgroup\$
  • \$\begingroup\$ That is much simpler, however we haven't really learned anything about data structs, arrays, or loops yet. I took the class a long time ago but can't fully remember what we learned. I will keep this in mind for the future if I need to build another one! \$\endgroup\$ – Alex Apr 22 '15 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.