mardi 20 janvier 2015

Returning void from method to prevent execution

I am attempting to exit a method which returns void after checking a condition (I realize this would be easier if I threw an exception, but I'm trying to avoid that for this project). I have the code below, which should return out of the if statement, if I am interpreting it correctly, but the entire method is still executing. Is there a problem in the code somewhere (I can post more if needed), or is there a better way to write this without exception handling?



void Rational::divide(Rational b) {
if ((double)b.numerator/(double)b.denominator == 0) {
cout << "Denominator cannot be zero." << endl;
return;
}
numerator = numerator * b.denominator;
denominator = denominator * b.numerator;
reduce();
}

Aucun commentaire:

Enregistrer un commentaire