Modify the code below so that the main() function calls the interim() function w
ID: 3534890 • Letter: M
Question
Modify the code below so that the main() function calls the interim() function which in turn calls the Quotient() functions. The Quotient() functions throws a DivByZero exception, the interim function re-throw it. The main() function then processes it.
#include<iostream>
#include <string>
using namespace std;
int Quotient(int, int);
class DivByZero // Exception class
{};
int main()
{
int numer; // Numerator
int denom; // Denominator
cout << "Enter numerator and denominator: ";
cin >> numer >> denom;
while (cin)
{
try
{
cout << "Their quotient: "
<< Quotient(numer, denom) << endl;
}
catch (DivByZero)
{
cout << "*** Denominator can't be 0"
<< endl;
}
cout << "Enter numerator and denominator: ";
cin >> numer >> denom;
}
return 0;
}
int Quotient(/*in */ int numer, // The numerator
/*in */ int denom) // The denominator
{
if (denom == 0)
throw DivByZero();
return numer/ denom;
}
//Or equivalently,
{
DivByZero x
if (denom == 0)
throw x;
return numer/ denom;
}
….
class MathError {};
….
….Function1(……)
{
try
{
//Call a function
}
catch(MathError)
{
//Error handling
}
}
…
….functionN(…)
{
…
if (….)
throw( MathError() );
….
}
int main(……)
{
string s1, s2, s3;
s1 = ……;
s2 = ……;
try
{
s3 = s1+s2;
}
catch(length_error)
{
cout << “String too longâ€;
return 1;
}
cout << s3;
….
return 0;
}
Explanation / Answer
Please rate with 5 stars :)
I am giving you the flow of how your code should written:
int main()
{
int numer;
int denom;
cout << "Enter numerator and denominator: ";
cin >> numer >> denom;
interim();
return 0;
}
Also interim() would look like this:
void interim()
{
while (cin)
{
try
{
cout << "Their quotient: "
<< Quotient(numer, denom) << endl;
}
catch (DivByZero)
{
cout << "*** Denominator can't be 0"
<< endl;
}
cout << "Enter numerator and denominator: ";
cin >> numer >> denom;Â
}
}
Cheers!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.