#include <iostream>
using namespace std;

int main()
{

	double firstNo;// first number is stored here
	double secondNo;
	double result;//variable for result when first and second number are operated upon
	char operation;// place to store the user's inputed operation
	

	// asks the user to choose the first number
	cout<< "Enter in the first Number ->";
	cin >> firstNo;

	// asks the user for the second number
	cout<< "Enter in the second Number -> ";
	cin >> secondNo;

	// asks the user for an operation to preform
	cout<< "which operation would you like to choose?\n";
	cout<< "(+) add, (-) sub, (*)multiply, (/) divide";
	cin >> operation;

	// if else statements that determine which operation is going to be preformed on numbers'
	if (operation == '+'){
		result = firstNo + secondNo;
	cout<< "The sum of these two numbers are ->" << result << "\n";
	}
	else if (operation == '-'){
		result = firstNo - secondNo;
	cout<< "The difference of these two numbers are ->" << result << "\n";
	}
	else if (operation == '*'){
		result = firstNo * secondNo;
	cout<< "The product of these two numbers are ->" <<result << "\n";
	}
	else {
		result = firstNo / secondNo;
	cout<< " The quotient of these two numbers are ->" << result << "\n";
	}
	

	cout<<"/n";

return 0;

}

I need to catch a division by zero so i can tell the user "you cannot divide by zero" instead of using if then statements i was wondering how may i go about this using a try- catch statement?

You need to write the code yourself. If you want to blow them out of the water then check if secondNo is zero and if so exit the program. If you want to be user friendly then you can detect that secondNo is zero and use a loop to continually ask the user to enter a nonzero value for seconNo.

I believe this thread is resolved, then. Please mark it as solved at the end of a thread.

Do you know of exceptions? You have to check for denominator and if it is zero, then throw exception. Then at some point in your program you will be catching exceptions including this one. Google for exception handling in C++ and see Deitel tutorial on that

>You have to check for denominator and if it is zero, then throw exception.
I wouldn't use an exception in this case. Perhaps an assertion, because the user's input should be checked and sanitized long before the division takes place. In which case division by zero would be a programmer error in gracefully handling bad input rather than a truly exceptional runtime situation.

>You have to check for denominator and if it is zero, then throw exception.
I wouldn't use an exception in this case. Perhaps an assertion, because the user's input should be checked and sanitized long before the division takes place. In which case division by zero would be a programmer error in gracefully handling bad input rather than a truly exceptional runtime situation.

You are right Narue, but his use of "catch" phrase gave me impression he is talking about exceptions ;)

Try this !!!


#include <iostream>

using namespace std;

int main()

{

double firstNo;// first number is stored here

double secondNo;

double result;//variable for result when first and second number are operated upon

char operation;// place to store the user's inputed operation

// asks the user to choose the first number

cout<< "Enter in the first Number ->";

cin >> firstNo;

// asks the user for the second number

cout<< "Enter in the second Number -> ";

cin >> secondNo;

// asks the user for an operation to preform

cout<< "which operation would you like to choose?\n";

cout<< "(+) add, (-) sub, (*)multiply, (/) divide";

cin >> operation;

// if else statements that determine which operation is going to be preformed on numbers'

if (operation == '+'){

result = firstNo + secondNo;

cout<< "The sum of these two numbers are ->" << result << "\n";

}

else if (operation == '-'){

result = firstNo - secondNo;

cout<< "The difference of these two numbers are ->" << result << "\n";

}

else if (operation == '*'){

result = firstNo * secondNo;

cout<< "The product of these two numbers are ->" <<result << "\n";

}

else {


if(secondNo==0)
{
cout<<"can not divide by zero";
}
else
{
result = firstNo / secondNo;

cout<< " The quotient of these two numbers are ->" << result << "\n";

}

}

cout<<"/n";

return 0;

}

I was thinking he thought dividing by zero would throw an exception as with C# and probably other .NET languages. It doesn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.