Error in logic

Ok so I am trying to get a error checker in my class to see if an expression enter by a user has a division by 0 in it; so I have this so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Divide: public SubExpression{
	
public:
    Divide(Expression* left, Expression* right): SubExpression(left, right)
    {
    }
    double evaluate()
    {
		if(right != 0){ //see if the right expression is 0
       return left->evaluate() /  right->evaluate();
		}
		else{
		cout << "Division by 0 error!" << endl;
		}
	}
};

I cant figure out what I need to do to see if there is a division by zero,
Thanks
If I'm not mistaken, line 9 is checking if the right POINTER is zero (i.e. null).

I think you want to know if the right expression (which I'm assuming is the denominator) EVALUATES to zero. Perhaps this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Divide: public SubExpression
{
public:
	Divide(Expression* left, Expression* right): SubExpression(left, right)
	{
	}
	double evaluate()
	{
		if(right->evaluate() != 0){ //see if the right expression is 0
			return left->evaluate() /  right->evaluate();
		}
		else{
			cout << "Division by 0 error!" << endl;
		}
	}
};
Last edited on
Yes that fixed it just right thanks a whole lot:)
Topic archived. No new replies allowed.