Question

In: Computer Science

In the main(), below, write a program that uses a switch-statement to choose between three numbers....

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;

}

Solutions

Expert Solution

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!


Related Solutions

In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                         Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward”
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                         Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward”
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                         Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward”
Have a look at the code below. Write a switch statement to check for the mode...
Have a look at the code below. Write a switch statement to check for the mode type. When the designated mode is matched, display an appropriate string for that mode. The default case should display the statement, “We do not offer this mode of course delivery.” public class SwitchCourse {     enum CourseMode { ONLINE, RESIDENTIAL, HYBRID }     CourseMode myCourse = CourseMode.HYBRID;        public void myCourse()     {         switch (/*___*/)         {             case ONLINE:                 System.out.println("You...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Please write a Java program using a for loop with a switch statement to enter 5...
Please write a Java program using a for loop with a switch statement to enter 5 quiz scores. Each should be out of ten points and give the corresponding grade A, B , C etc.
What is the main differences between Packet Switch Network & Circuit Switch Network?
What is the main differences between Packet Switch Network & Circuit Switch Network?
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter...
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter a radius. Then present a menu of choices for quantities to be calculated from that radius:                         A         Area of a Circle                         C         Circumference of a Circle                         S          Surface Area of a Sphere                         V         Volume of a Sphere Prompt the user to enter the character corresponding to the quantity to be calculated. Use a switch statement to handle the calculations. Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT