Objectives
After this lab assignment, students should be able to:
- Declare and initialize C++ structs
- Implement designs using structs
Instructions
In this lab, we will create an abstraction of a vending machine in C++, using structs to describe each item in the machine, and a vector to maintain a list of all the items in the machine.
Use the following struct declaration for vending machine items:
1 | struct item { |
Use the following vector declaration for the vending machine:
1 | int main() { |
Complete the three following function headers shown below:
1 | float totalValue(vector<item> vendingMachine) |
1 | void stock(string name, int count, vector<item> &vendingMachine) |
1 | void purchase(float balance, int code, vector<item> &vendingMachine) |
Testing the Functions
1 | int main() { |
Sample Output
The item you are adding, Doublemint Gum, was not in the machine.Please enter the following information for this itemCode: 100Price: $0.75You have added 1 units of Doublemint Gum. Now there are 1 units in the machine------------------You have selected to purchase Doublemint Gum, which costs $0.75Your balance is $0.50. Not enough money!------------------The item you are adding, Chocolate Chip Cookies, was not in the machine.Please enter the following information for this itemCode: 100That code is already being used by Doublemint Gum.Enter a new code:101Price: $2.00You have added 10 units of Chocolate Chip Cookies. Now there are 10 units in the machine------------------You have added 5 units of Chocolate Chip Cookies. Now there are 15 units in the machine------------------You have selected to purchase Doublemint Gum, which costs $0.75Thank you for purchasing! Your change is: $0.00------------------Item not found!------------------This vending machine now has $30.00 worth of itemsPress any key to continue ...Submission
Call your program VendingMachine.cpp. This file must contain all of the following items:
- Completed function definitions:
- totalValue()
- purchase()
- stock()
- Declaration of struct item
- main() function with same code found in the Testing the Functions section above
- A sample output of your program, like shown in the Sample Output section above (the output does not have to match exactly)
Submit VendingMachine.cpp to Canvas before the deadline.
