Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using c++ Exception Handling Division by Zero & Invalid Input Exceptions DownloadmainProgram.cpp and divisionByZero.h from Blackboard. mainProgram.cpp mimics a simple calculator. It takes as

using c++ Exception Handling

Division by Zero & Invalid Input Exceptions • Download"mainProgram.cpp" and "divisionByZero.h" from Blackboard. •"mainProgram.cpp" mimics a simple calculator. It takes as input twointegers and an arithmetic operation (+, -, *, or /) is performed.It then outputs the numbers, the operator, and the result. • Fordivision, if the denominator is zero, this program("mainProgram.cpp") only outputs a message. • Modify this programso that it handles exceptions of "division by zero" and "invalidinput". – Throw an object of class divisionByZero to handle theexception of "division by zero". This class is defined in"divisionByZero.h" – Throw an object of class String to handle theexception of "invalid input". • To handle exception of "division byzero", in the corresponding catch block, the program should ask theuser to input another value for the divisor. To do so, the program– first calls "what" function of class divisionByZero, – clears theerror flag on "cin" by: cin . cl e a r ( ) ; – skips to the nextnewline by (We assume there is only 100 characters to skip in eachline): cin . ignore (100 , ’ ’ ) ;

– finally, asks the user to input another value for divisor. •To handle exception of "invalid input", in the corresponding catchblock, the program displays an error message on the console andterminates the program. • Additional requirements and instructions:– No use of global variables. – Code is well commented. – Commentsfor functions describe what they do. They also describe the postconditions. If parameter names are not descriptive in functionheaders, the comments for a function describe the pre conditions aswell.

----------------------------------------
here are the .cpp

---------------------------------------

#include #include using namespace std; int main(){     int num1, num2;    char opr;    cout << "Enter two integers: ";    cin >> num1 >> num2;    cout << endl;    cout << "Enter operator: + (addition), - (subtraction),"         << " * (multiplication), / (division): ";    cin >> opr;    cout << endl;    cout << num1 << " " << opr << " " << num2 << " = ";    switch (opr)    {    case '+':         cout << num1 + num2 << endl;        break;        case '-':         cout << num1 - num2 << endl;        break;        case '*':         cout << num1 * num2 << endl;        break;        case '/':         if (num2 != 0)            cout << num1 / num2 << endl;        else            cout << "ERROR Cannot divide by zero" << endl;        break;        default:          cout << "Illegal operation" << endl;        }        return 0;}
--------------------------
// User-defined exception class.  #include #include   using namespace std;class divisionByZero                          //Line 1{                                             //Line 2public:                                       //Line 3    divisionByZero()                          //Line 4        {                message = "Division by zero";         //Line 5        }                                         //Line 6        divisionByZero(string str)                //Line 7        {                                         //Line 8                message = str;                        //Line 9        }                                         //Line 10        string what()                             //Line 11        {                                         //Line 12                return message;                       //Line 13        }                                         //Line 14private:                                      //Line 15        string message;                           //Line 16};                                            //Line 17

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

What is the adjusted present value of this project? LO.1

Answered: 1 week ago