Case Study: Counting Monetary Units in C++

This section presents a program that breaks a large amount of money into smaller units.

Suppose you want to develop a program that changes a given amount of money into smaller monetary units. The program lets the user enter an amount as a double value representing a total in dollars and cents, and outputs a report listing the monetary equivalent in the maximum number of dollars, quarters, dimes, nickels, and pennies, in this order, to result in the minimum number of coins , as shown in the sample run.

Here are the steps in developing the program:

  1. Prompt the user to enter the amount as a decimal number, such as 11.56.
  2. Convert the amount (e.g., 11.56) into cents (1156).
  3. Divide the cents by 100 to find the number of dollars. Obtain the remaining cents using the cents remainder 100.
  4. Divide the remaining cents by 25 to find the number of quarters. Obtain the remaining cents using the remaining cents remainder 25.
  5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining cents using the remaining cents remainder 10.
  6. Divide the remaining cents by 5 to find the number of nickels. Obtain the remaining cents using the remaining cents remainder 5.
  7. The remaining cents are the pennies.
  8. Display the result.

The complete program is given in Listing 2.12.

Listing 2.12  ComputeChange.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    // Receive the amount

7    cout << “Enter an amount in double, for example 11.56: “;

8    double amount;

9    cin >> amount;

10

11    int remainingAmount = static_cast<int>(amount * 100);

12

13    // Find the number of one dollars

14    int numberOfOneDollars = remainingAmount / 100;

15    remainingAmount = remainingAmount % 100;

16

17    // Find the number of quarters in the remaining amount

18    int numberOfQuarters = remainingAmount / 25;

19    remainingAmount = remainingAmount % 25;

20

21    // Find the number of dimes in the remaining amount

22    int numberOfDimes = remainingAmount / 10;

23    remainingAmount = remainingAmount % 10;

24

25    // Find the number of nickels in the remaining amount

26    int numberOfNickels = remainingAmount / 5;

27    remainingAmount = remainingAmount % 5;

28

29    // Find the number of pennies in the remaining amount

30    int numberOfPennies = remainingAmount;

31

32    // Display results

33    cout << “Your amount ” << amount << ” consists of ” << endl <<

34    ” ” << numberOfOneDollars << ” dollars” << endl <<

35    ” ” << numberOfQuarters << ” quarters” << endl <<

36    ” ” << numberOfDimes << ” dimes” << endl <<

37    ” ” << numberOfNickels << ” nickels” << endl <<

38    ” ” << numberOfPennies << ” pennies” << endl;

39

40    return 0;

41 }

 The variable amount stores the amount entered from the keyboard (lines 7–9). This variable should not be changed, because the amount has to be used at the end of the program to display the results. The program introduces the variable remainingAmount (line 11) to store the changing remainingAmount

The variable amount is a double decimal representing dollars and cents. It is converted to an int variable remainingAmount, which represents all the cents. For instance, if amount is 11.56, then the initial remainingAmount is 1156. The division operator yields the inte­ger part of the division. So 1156 / 100 is 11. The remainder operator obtains the remainder of the division. Therefore, 1156 % 100 is 56.

The program extracts the maximum number of dollars from the total amount and obtains the remaining amount in the variable remainingAmount (lines 14-15). It then extracts the maxi­mum number of quarters from remainingAmount and obtains a new remainingAmount (lines 18-19). Continuing the same process, the program finds the maximum number of dimes, nickels, and pennies in the remaining amount.

One serious problem with this example is the possible loss of precision when casting a double amount to an int remainingAmount. This could lead to an inaccurate result. If you try to enter the amount 10.03, then 10.03 * 100 becomes 1002.9999999999999. You will find that the program displays 10 dollars and 2 pennies. To fix the problem, enter the amount as an integer value representing cents (see Programming Exercise 2.24).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *