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...
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...
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...
With C code Write a switch statement (not a complete program) which prints an appropriate message...
With C code Write a switch statement (not a complete program) which prints an appropriate message for a letter code entered. Use the following messages: If L is entered, output the message "Lakers" If C is entered, output the message "Clippers" If W is entered, output the message "Warriors" If any other character is entered, output the message "invalid code" Make sure to handle the case where the user enters in a small letter. That is, a capital or small...
What is the main differences between Packet Switch Network & Circuit Switch Network?
What is the main differences between Packet Switch Network & Circuit Switch Network?
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT