Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Exercise 14-2 Instructions Redo Programming Exercise 8 of Chapter 4 so that your program handles exceptions such as division by zero and invalid input.

Programming Exercise 14-2

Instructions

Redo Programming Exercise 8 of Chapter 4 so that your program handles exceptions such as division by zero and invalid input.

Your program should print Division by zero when 0 is entered for a denominator.

divisionByZero.h:

#include

#include //this header is for conversion to float

#include "divisionByZero.h"

using namespace std;

int main() {

//using string variables to read input and convert numerator and denominator to float

string num,den;

float numerator, denominator;

//bool falgs to check user input is valid and and also if den is 0

bool bool_value, num_flag = false , den_flag = false;

//reading numerator

cout << "Please enter the numerator : ";

cin >> num;

numerator = atof(num.c_str());

cout << numerator<< endl;

//validating the read value of num

while(true)

{

//if valid then we will come out of loop

if(not(num != "0" && numerator == 0))

{

num_flag = true;

break;

}

else

{

//else we will read until user enters correct value

cout << "inavlid input ";

cout << "Please enter the numerator : ";

cin >> num;

numerator = atof(num.c_str());

cout << numerator<< endl;

}

}

//reading denominator as a string into variable den

cout << "Please enter the denominator : ";

cin >> den;

//converting to float

denominator = atof(den.c_str());

cout << denominator<< endl;

//validating user input same as numerator

while(true)

{

if(not(den != "0" && denominator == 0))

{

den_flag = true;

break;

}

else

{

cout << "inavlid input ";

cout << "Please enter the denominator : ";

cin >> den;

denominator = atof(den.c_str());

cout << denominator<< endl;

}

}

//calling checkDenominator method which is in divisionByZero.h file to check denominator value is 0 or not

bool_value =checkDenominator(denominator);

//if denominator is not zero then we will show division

if(bool_value == false )

{

cout <<"The value of numerator/denominator is : " <

}

}

main.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;

}

Division by zero error is shown

1

0 out of 1 checks passed. Review the results below for more details.

Checks

Test CaseIncomplete

Program produces error message

Input

3 0 / 1 

Output

 

Results:

Division by zero

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899