Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started