C++ || Cash Register Simulation – Display The Total Sales Amount In Dollars & Cents Using Modulus

Print Friendly, PDF & Email

The following is a simple program which demonstrates more use of the modulus (%) function to manipulate integer data.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Modulus
Type Casting - Int
The Value Of U.S Currency

This program first prompts the user to enter in a monetary amount into the system. This number can be a decimal number, or a whole number. Once the user enters in an amount, the program will use the modulus operator to determine exactly how many 1 dollar bills, quarters, dimes, nickles, and pennies consisted of the amount that the user entered into the program. So for example, if the user entered the value of 2.34, the program would display the result of 2 dollars, 1 quarters, 0 dimes, 1 nickels, and 4 pennies.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

====== RUN 1 ======

Enter the total sales amount in dollars & cents (for example 19.87): 19.87

The amount of $19.87 consists of:
19 dollar(s)
3 quarter(s)
1 dime(s)
0 nickel(s)
2 pennie(s)

====== RUN 2 ======

Enter the total sales amount in dollars & cents (for example 19.87): 11.93

The amount of $11.93 consists of:
11 dollar(s)
3 quarter(s)
1 dime(s)
1 nickel(s)
3 pennie(s)

====== RUN 3 ======

Enter the total sales amount in dollars & cents (for example 19.87): 3.00

The amount of $3 consists of:
3 dollar(s)
0 quarter(s)
0 dime(s)
0 nickel(s)
0 pennie(s)

Leave a Reply