Using C++ program Use throw, try, catch to implement the following. 1. the quest
ID: 3778063 • Letter: U
Question
Using C++ program
Use throw, try, catch to implement the following.
1. the question appears in the console, 'Do you want to be NICE or EVIL? Enter an integer between 0 - 10 to represent the answer. 1 means super EVIL and 10 means super NICE.'
2. User inputs an integer.
3. If this number is smaller that 5, there are suggesstions pop up in the console: ' Think twice, why not choose to be nice?'
4. When the user enters a number that is equal to or greater than 5, words pop up in the console as "Good job! Thank you!"
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <exception>
using namespace std;
int main()
{
//Declaring variables
int number;
while(true)
{
cout<<"Do you want to be NICE or EVIL? "<<endl;
//Getting the how many numbers user want to enter
cout<<" Enter an integer between 0 - 10:";
cin>>number;
try
{
if(number==1)
{
cout<<"Super Evil"<<endl;
break;
}
else if(number==2)
{
cout<<"Super Nice"<<endl;
break;
}
else if(number<5)
{
throw "Think twice, why not choose to be nice?";
}
else if(number>=5)
{
cout<<"Good Job Thank You"<<endl;
break;
}
}
catch (const char* strException)
{
//Displaying the Exception
cerr << "Error: " << strException << endl;
continue;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.