HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Bills and coin denomination program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
coinprogramdenominationandbills

Problem

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

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 > 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: " << Nicke

Solution

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 

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 > 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.

Code Snippets

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

Context

StackExchange Code Review Q#87494, answer score: 2

Revisions (0)

No revisions yet.