Write the C++ code which prompts the user for an int value between 1 and 4 (incl
ID: 3670876 • Letter: W
Question
Write the C++ code which prompts the user for an int value between 1 and 4 (inclusive). The word for the number read is to be printed to the screen (eg. if the user entered a 1, the word ONE is printed). If a value out of legal range was entered an error message is to be given. Use a switch statement in your solution.
Help please! I know it's extremely simple, but I can't seem to understand switch statements. Could someone write the program so I could review how it works. I have a test tomorrow and this is a grey area for me. Thanks!
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n;
cout <<"Enter a Number";
cin >>n;
switch(n){
case 1: cout<< " One";break;
case 2: cout<< " Two";break;
case 3: cout<< " Three";break;
case 4: cout<< " Four";break;
default:cout<<"Error not in the range";break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.