If you do not have a CSD account yet, you may create it with the instructions here. Please pay special attention to which two checkboxes you need to check. You may already have it if you have studied some other COMP courses at HKUST.
VSCode will be used as the IDE (Integrated Development Environment) for COMP2011, COMP2012, and COMP2012H. Please check here for more information about setting up VSCode.
To express condition/s that we will use to control program flow, relational and logical operators can be used.
To select or to branch according to certain condition/s, the following selection/branching statements can be used.
To iterate or to loop according to certain condition/s, the following iteration/looping statements can be used. The break and continue statements provide additional control to these iteration/looping statements.
This is a lab on program flow control.
In this lab, we are going to implement a simple vending machine. We assume the vending machine is one of the old ones where users insert coins in order to purchase items. So, a user can interact with the vending machine by performing one of three actions, until the user leaves:
To support above operations, internal states are maintained in the vending machine:
Since this is a lab on program flow control, please complete the lab without the following features:
To represent internal states, define a variable for each internal state.
For example, define an int variable for balance. Also, define three int variables, for the count of each kind of coin, and so on:
int balance = 0;
int coin_5_count = 3;
int coin_2_count = 3;
int coin_1_count = 3;
/* more... */
For best practice, avoid defining global variables. Please define the variables inside the main function.
When the program is started, display the following menu, including the balance, then prompt the user to provide input. This is repeated forever after taking each action until the user decides to leave. (All menu text cout statements are given in the program skeleton, see Resources & Sample I/O)
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4:
The user input needs to be validated, though it can be assumed that the user always enters an integer. Suppose the user has entered 5, which is invalid. In this case, report invalid input to the user and prompt the user for input again:
Please enter 1-4: 5
Invalid choice
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4:
If the user decided to insert a coin, then prompt the user for the coin:
Please insert a coin: $
If the user did not supply a $5, $2 or a $1 coin, then report invalid input to the user and prompt the user for input again:
Please insert a coin: $3
Only $5, $2 and $1 coins are accepted
Please insert a coin: $
Otherwise, increase the balance and the count of the corresponding coin.
If the user decided to purchase, then display the following submenu to show each item and the cost, also a menu option to go back without purchasing. If an item has been sold out (count of that item is zero), then display "SOLD OUT" for that item. Lastly, prompt the user for input:
Please select an item:
1. Item A - $6
2. Item B - SOLD OUT
3. Go back
Please enter 1-3:
If the chosen item has been sold out, report this to user and prompt the user for input again. If the balance is smaller than the cost of the chosen item, report insufficient balance to user and prompt the user for input again. Also, the input needs to be validated:
Please enter 1-3: 2
Item B has been sold out
Please select an item:
1. Item A - $6
2. Item B - SOLD OUT
3. Go back
Please enter 1-3: 1
Insufficient balance
Please select an item:
1. Item A - $6
2. Item B - SOLD OUT
3. Go back
Please enter 1-3: 4
Invalid choice
Please select an item:
1. Item A - $6
2. Item B - SOLD OUT
3. Go back
Please enter 1-3:
Otherwise, decrease the balance and the count of the corresponding item. Also, report to user the purchased item:
Please enter 1-3: 1
Purchased item A
If the user decided to return coins, then attempt to return an available coin with the largest value not exceeding the current balance, until the balance reaches zero or there are no available coins to return. Report to user for each returned coin.
Suppose the balance is $6,
Returned a $5 coin
Returned a $1 coin
Returned a $2 coin
Returned a $2 coin
Returned a $2 coin
Returned a $2 coin
Returned a $2 coin
Returned a $1 coin
Returned a $1 coin
Returned a $5 coin
Oops... Not enough coins to return...
The remaining balance is $1.
If the user decided to leave, then print the following message and terminate the program.
Bye
A sample I/O is given below, with the user input highlighted. You are encouraged to try the demo program to try more possible cases.
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $5
The balance is $5
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 2
Please select an item:
1. Item A - $6
2. Item B - $4
3. Go back
Please enter 1-3: 2
Purchased item B
The balance is $1
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 3
Returned a $1 coin
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $5
The balance is $5
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 2
Please select an item:
1. Item A - $6
2. Item B - $4
3. Go back
Please enter 1-3: 2
Purchased item B
The balance is $1
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 3
Returned a $1 coin
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $5
The balance is $5
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 2
Please select an item:
1. Item A - $6
2. Item B - $4
3. Go back
Please enter 1-3: 2
Purchased item B
The balance is $1
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 3
Returned a $1 coin
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $5
The balance is $5
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $2
The balance is $7
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 2
Please select an item:
1. Item A - $6
2. Item B - SOLD OUT
3. Go back
Please enter 1-3: 1
Purchased item A
The balance is $1
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 3
Oops... Not enough coins to return...
The balance is $1
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 1
Please insert a coin: $1
The balance is $2
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 3
Returned a $2 coin
The balance is $0
Please choose an action:
1. Insert coin
2. Purchase
3. Return coins
4. Leave
Please enter 1-4: 4
Bye
lab1.cpp and zip it as lab1.zip for submission to ZINC.
lab1.cpp otherwise ZINC cannot find the file.lab1.cpp, not a folder containing lab1.cpp.Here are the test cases used in ZINC: