68.2k views
2 votes
In C++

Write a switch statement that checks nextChoice. If 0, print "Rock". If 1, print "Paper". If 2, print "Scissors". For any other value, print "Unknown". End with newline.

1 Answer

4 votes

switch(nextChoice) {

case 0:

cout << "Rock" << endl;

break;

case 1:

cout << "Paper" << endl;

break;

case 2:

cout << "Scissors" << endl;

break;

default:

cout << "Unknown" << endl;

}

This switch statement checks the value of the variable nextChoice. If the value is 0, it will print "Rock" and end the switch statement with the break statement. If the value is 1, it will print "Paper" and end the switch statement. If the value is 2, it will print "Scissors" and end the switch statement. For any other value, the default case will be executed, printing "Unknown" and end the switch statement.

User Bilal Maqsood
by
8.0k points