Answer to Question #273284 in C++ for mannya

Question #273284

1. Accept two numbers a,b from the user and print a/b. If b is entered as zero, handle the exception and print appropriate message. Use, try, catch ,throw keywords



1
Expert's answer
2021-11-29T16:15:37-0500
#include<iostream>
using namespace std;

int main()
{
	double a, b;
	do
	{
		cout << "\nPlease, enter a number a (0 - exit program): ";
		cin >> a;
		if (a == 0)break;
		cout << "Please, enter a number b: ";
		try
		{
			cin >> b;
			if (b == 0)
				throw "Division by zero!";
			cout << "Result of division is " << a / b;
		}
		catch (const char* exception)
		{
			cerr << "Error: " << exception << endl;
		}
	} while (true);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS