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...
The notation anArray[____] can always be used to access the last element of anArray.
A. anArray.max-1 B. anArray.max C. anArray.length-1 D. anArray.length
What type of penetration testing technique is used if the tester has no prior knowledge of the network infrastructure that is being tested?
A. white box B. gray box C. black box D. sealed box