Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE C++ ONLY Redo programming exercise 8 of chapter 4 so that your program ha

ID: 3860866 • Letter: P

Question

PLEASE C++ ONLY

Redo programming exercise 8 of chapter 4 so that your program handles exceptions such as division by zero and invalid input. Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message). Some sample outputs follow:

3 + 4 = 7

13 * 5 = 65

REFERENCE CODE

using namespace std;

int main()

{

inta, b;

char ch;

try

{

cout << "Enter two numbers: ";

cin >> a >> b;

cout <

if (b==0)

throw b;

cout << " 1. Addition 2.Substraction 3. Multiply 4. Division 5.Modulus" <

cout << " Enter your choice (+ or - or * or %): ";

cin >> ch;

switch (ch)

{

case'1': cout << " Add="<

break;

case'2': cout << " Sub="<

break;

case'3': cout << " Mul="<

break;

case'4': cout << " Div="<

break;

case'5': cout << " Mod="<

break;

default: throw ch;

}

}

catch(int b)

{

cout<"<

}

catch(char ch)

{

cout<"<

}

system("pause")

}

Explanation / Answer

#include <iostream>
using namespace std;
int main()
{
int a, b;
char ch;
try {
cout << "Enter two numbers: ";
cin >> a >> b;
if (b == 0) throw b;
cout << " 1.Addition 2.Substraction 3.Multiply 4.Division 5.Modulus ";
cout << " Enter your choice (+ or - or * or %): ";
cin >> ch;
switch (ch) {
case '1':
cout << a << " + " << b << " = " << a+b;
break;
case '2':
cout << a << " - " << b << " = " << a-b;
break;
case '3':
cout << a << " * " << b << " = " << a*b;
break;
case '4':
cout << a << " / " << b << " = " << a/b;
break;
case '5':
cout << a << " % " << b << " = " << a%b;
break;
default:
throw ch;
}
}
catch (int b) {
cout << "Denominator cannot be zero " << endl;
}
catch (char ch) {
cout << "Invalid selection" << endl;
}
}
/*
sample output

Enter two numbers: 3 4
1.Addition
2.Substraction
3.Multiply
4.Division
5.Modulus
Enter your choice (+ or - or * or %): 3
3 * 4 = 12

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote