C++,Struct。
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 6
|
struct item #123; string name; int code; float price; int stock; #125;;
|
Use the following vector declaration for the vending machine:
1 2
|
int main() #123; vectorlt;itemgt; vendingMachine;
|
Complete the three following function headers shown below:
1 2
|
float totalValue(vectorlt;itemgt; vendingMachine)
|
1 2 3 4
|
void stock(string name, int count, vectorlt;itemgt; amp;vendingMachine)
|
1 2 3 4 5 6 7
|
void purchase(float balance, int code, vectorlt;itemgt; amp;vendingMachine)
|
Testing the Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main() #123; vectorlt;itemgt; vendingMachine; stock("Doublemint Gum", 1, vendingMachine); cout lt;lt; "-------------------" lt;lt; endl; purchase(.50, 100, vendingMachine); cout lt;lt; "-------------------" lt;lt; endl; stock("Chocolate Chip Cookies", 10, vendingMachine); cout lt;lt; "-------------------" lt;lt; endl; stock("Chocolate Chip Cookies", 5, vendingMachine); cout lt;lt; "-------------------" lt;lt; endl; purchase(.75, 100, vendingMachine); cout lt;lt; "-------------------" lt;lt; endl; purchase(1.00, 100, vendingMachine); cout lt;lt; "This vending machine now has $" totalValue(vendingMachine) lt;lt; fixed lt;lt; setprecision(2) lt;lt; " worth of items" lt;lt; endl; #125;
|
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.