Description
The purpose of this challenge is to use function parameters passed by reference. This challenge simulates a coin change return machine like the ones found at the grocery store.
Requirements
- Write a function called double get_currency(string prompt). This function will prompt the user to enter a positive monetary value. This function should continue to prompt the user as long as negative numbers are entered. The prompt parameter is the text that is displayed to the user. This function returns the value that the user enters. It’s OK to use cin/cout in this function.
- Write an overloaded function called double get_currency(string prompt, double minimum). Notice that the function name here is the same as in Step 1, but the parameter lists are different. This function asks the user for a value that is at least the value of minimum. If the user enters a number that’s less than the minimum, then display an error message (think input validation, See sample interaction). This function returns the value that the user enters. It’s OK to use cin/cout in this function.
- In main(), use the function get_currency() to ask the user to enter a total. See below:
total = get_currency("Enter a total: ");
- In main(), use the function get_currency() to ask the user to enter the amount tendered, passing in total as the second parameter. Basically, you will not allow the user to give money that is less than the amount owed.
tendered = get_currency("Enter amount tendered: ", total);
- Declare int quarters, dimes, nickels, pennies in main. These will be used as variables to be passed by reference in the calc_change() function. Initialize all of these variables to zero before passing them into the calc_change() call.
- Declare a function void calc_change(double change, int & quarters, int & dimes, int & nickels, int & pennies). The calc_change() function will calculate how many coins will be needed as change. The value change contains the amount of change; this amount can contain dollars and cents. When calculating how many coins for each of the denominations, make sure to only calculate for the actual coins (think of the grocery store, if your change is $1.66, you get the coins equivalent of 66 cents. The $1.00 is given as dollar bills. It is the 66 cents that you want to split up into quarters, nickels, etc). HINT: declare an int variable called cents. Set cents = change * 100; // why?
- Remember that main() will be responsible for displaying the results of calc_change(). (DO NOT have any couts in calc_change())
- Programming with monetary amounts using doubles can sometimes result in off-by-a-penny calculations. In real world scenarios, there are techniques to circumvent these types of issues. In this assignment, if you do encounter being off by 1 cent, you may ignore it.
Sample/partial main()
int main() { double total, tendered; int quarters = 0, dimes = 0, nickels = 0, pennies = 0; total = get_currency("Enter a total: "); tendered = get_currency("Enter amount tendered: ", total); calc_change(tendered - total, quarters, dimes, nickels, pennies); // more code return 0; }
Sample Interaction / Output
Enter a total: 20.34 Enter amount tendered: 20.00 *** Amount must be at least 20.34 Enter amount tendered: 22.00 Your change is $1.66. You should have 2 quarter(s), 1 dime(s), 1 nickel(s), and 1 penny/ies.
Extra Challenge
Have your output be English-friendly, as below:
You should have 2 quarters, 1 dime, 1 nickel, and 1 penny.
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0006d
Print Requirements