In: Computer Science
In the main(), below, write a program that uses a switch-statement to choose between three numbers. The number, choice, should be prompted for and each choice should print out a unique statement. Also, the prompt should continue indefinitely (i.e. keep prompting until the user force-quits the program), and there should be a general catch-all if the user enters other than the three possible choices.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
<your answer goes here>
return 0;
}
Have a look at the below code. I have put comments wherever required for better understanding.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Choose any number among 4,7 & 10"<<endl;
// intialize the variable to store user input...
int a;
// take user input
cin >> a;
try {
// put conditional statements for 3 numbers
if(a==4) cout << "4 is an even number"<<endl;
else if(a==7) cout << "7 is an odd number"<<endl;
else if(a==10) cout << "10 is a multiple of 5"<<endl;
// for any other number throw Exception
else throw 's';
} catch(...) {
// give a valid message to the user
cout << "Caught Exception!\n";
cout<< "Print enter a valid number"<<endl;
}
return 0;
}
Happy Learning!