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
2
3
4
5
6struct item {
string name; // name of the item
int code; // numerical code entered to select the item
float price; // price per unit of the item
int stock; // number of items available in the machine
};
Use the following vector declaration for the vending machine:1
2int main() {
vector<item> vendingMachine;
Complete the three following function headers shown below:1
2float totalValue(vector<item> vendingMachine)
// return the total value of all items in the vending machine
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 item
Code: 100
Price: $0.75
You 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.75
Your 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 item
Code: 100
That code is already being used by Doublemint Gum.
Enter a new code:
101
Price: $2.00
You 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.75
Thank you for purchasing! Your change is: $0.00
------------------
Item not found!
------------------
This vending machine now has $30.00 worth of items
Press 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.