What is the incorrect action and why does it occur?
Specification: Check to see if the user’s input is 1, 2, or 3. Write out the numeric word (such as ONE) if it is within range; otherwise, write OUT OF RANGE.
```
#include
using namespace std;
int main()
{
int user_input;
cout << “\nEnter an integer.”;
cin >> user_input;
switch(user_input)
{
case 1:
cout << “\nONE”;
case 2:
cout << “\nTWO”;
case 3:
cout << “\nTHREE”;
default: cout << “OUT OF RANGE”;
}
return 0;
}
```
The problem is that if the user enters a 1, the program will write:
ONE
TWO
THREE
OUT OF RANGE
Or, if the user enters a 2, the program writes:
TWO
THREE
OUT OF RANGE
Or, if the user enters a 3, the program writes:
THREE
OUT OF RANGE
The case blocks do not contain break statements and thus, the program executes the lines of code from where it enters the first valid case statement until the end of the switch—or if it were to finally reach a break statement.
The fix is to add break statements in the three case blocks of code.
You might also like to view...
Answer the following questions true (T) or false (F)
1. It is proper for a recursion to run on without ending. 2. A proper recursive solution requires at least two cases: a recursive function that calls the recursive function with a smaller problem, and a base, or stopping case.
The smallest unit of main memory in most computers today is the ____, which consists of 8 bits.
Fill in the blank(s) with the appropriate word(s).
What is the effect of the following import statement?
``` import java.awt.*; ```
The _______________ is a priority-ordered list of the other carrier networks and frequencies it should search for when it cannot locate its home carrier?s network.
Fill in the blank(s) with the appropriate word(s).