Write a C++ program which will model accepting payment and giving change for a s
ID: 3668700 • Letter: W
Question
Write a C++ program which will model accepting payment and giving change for a snack vending machine. The machine will contain the following items, shown with the label used to select the item and its price:
- Throughout the program, all amounts of money must be stored internally as cents (in variables of type int).
- There must be three function with the following prototypes:
int menu(void);
int accept_money(int price);
int compute_change(int total_paid, int total_price);
These three function must behave as follows:
a) The function menu must display the list of snacks items and prompt the user for their choice. If the user makes a valid selection, the function must return the price of that selection, otherwise the user must be re-prompted for a choice until a valid choice from the menu is obtained.
b) The function accept_money must give the user a choice of coins to insert, and add up the total amount inserted by the user, in a loop until this total amount is equal to or greather than the function's parameter price (which is the cost of the user's choice of snack). Once this loop is complete, the function must return the total amount of money inserted by the user.
c) The function compute_change must return the amount of change owed to the user, found by subtracting total_price from total_paid.
- For any menu choice where a letter is expected, if a certain letter is a valid menu choice, then accept that letter in both upper- and lower-case.
- For the menus for selecting a snack and selecting an amount of money to insert, if an invalid selection is made, then re-prompt the user for a selection until a valid selection is made. For the choice of whether to purchase another snack, 'y' and 'Y' mean yes, and anything else means no.
- The main program must contain a loop that calls the functions menu, accept_money, and compute_change using suitable variables to store the amounts of money involved, then displays the total amount inserted and the changed returned. After completing a transaction, it must ask the user whether they whould like to make another purcahse, and contines as long as the user chooses to.
- Use comments to show the main steps in the program, and to document variable declarations.
- A sample run of your program should look like:
Welcome to the snack vending machine
Available snacks to select from:
P - Potato Chips $1.25
S - Snickers Bar $0.95
C - Cookies $1.50
B - Brownie $1.75
N - Nuts $1.40
Please enter the letter labeling your snack selection: P
Money accepted by the machine:
N - Nickel
Q - Quarter
D - Dollar
Your selected snack item cost: 125 CENTS
Your total inserted: 0 CENTS
Insert amount (enter letter of choice): d
Your selected snack item cost: 125 CENTS
Your total inserted: 100 CENTS
Insert amount (enter letter of choice): D
Your total inserted: 200 CENTS
Dispensing change: 75 CENTS
Would you care to make another purchase (Y/N): n
Thank you and enjoy your purchases!
Explanation / Answer
Class Attribute Names
Justification
Example
Method Argument Names
Justification
Example
Variable Names on the Stack
Justification
Example
The standard pointer notation is not entirely satisfactory because it doesn't look quite right, but it is consistent.
How do you handle statics? There's never a reason to have a static local to a function so there's no reason to invent a syntax for it. But like for most absolute rules, there is an exception, that is when making singletons. Use a "s_" prefix in this case. Take a look at Singleton Pattern for more details.
Pointer Variables
Justification
Example
Reference Variables and Functions Returning References
Justification
Example
Global Variables
Justification
Example
Global Constants
Justification
It's tradition for global constants to named this way. You must be careful to not conflict with other global #defines and enum labels.
Example
Static Variables
Justification
Example
Type Names
Justification
Example
Enum Names
Labels All Upper Case with '_' Word Separators
This is the standard rule for enum labels.
Example
Enums as Constants without Class Scoping
Sometimes people use enums as constants. When an enum is not embedded in a class make sure you use some sort of differentiating name before the label so as to prevent name clashes.
Example
Enums with Class Scoping
Just name the enum items what you wish and always qualify with the class name: Aclass::PIN_OFF.
Make a Label for an Error State
It's often useful to be able to say an enum is not in any of its valid states. Make a label for an uninitialized or error state. Make it the first label if possible.
Example
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.