Consider a vending machine that offers the following options: [1] Get gum [2] Get chocolate [3] Get popcorn [4] Get juice [5] Display total sold so far [6] Quit Design and implement a program that continuously allows users to select from these options. When options 1–4 are selected an appropriate message is to be displayed acknowledging their choice. For example, when option 3 is selected the following message could be displayed: Here is your popcorn When option 5 is selected, the number of each type of item sold is displayed. For example: 3 items of gum sold 2 items of chocolate sold 6 items of popcorn sold 9 items of juice sold When option 6 is chosen the program terminates. If an option other than 1 – 6 is entered an appropriate error message should be displayed, such as: Error, options 1-6 only!
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Consider a vending machine that offers the following options:
[1] Get gum
[2] Get chocolate
[3] Get popcorn
[4] Get juice
[5] Display total sold so far
[6] Quit
Design and implement a
these options. When options 1–4 are selected an appropriate message is to be
displayed acknowledging their choice. For example, when option 3 is selected the following message could be displayed: Here is your popcorn
When option 5 is selected, the number of each type of item sold is displayed. For example:
3 items of gum sold
2 items of chocolate sold
6 items of popcorn sold
9 items of juice sold
When option 6 is chosen the program terminates. If an option other than 1 – 6 is
entered an appropriate error message should be displayed, such as:
Error, options 1-6 only!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 5 images