Return an error and continue program?

I have some code that at times, will divide a number by 0, which is undefined. This still gives me an output of 1.$ or something, which I know represents undefined. However, I would like to print out an error message instead saying something like "cannot divide by 0", and continue the program because there are more things to do. How do I do this?
You can just include an if statement where you check if the divisor is zero and print out the message.

example.)

1
2
3
if(divisor == 0)
  std::cout << "Cannot divide by 0";
else return (dividend/divisor);
closed account (2LzbRXSz)
I'm not too sure what sort of program you're trying to make, but why not check if the number you're dividing is 0?

1
2
3
4
5
    int num = 0;
    if (num == 0)
    {
        cout << "Cannot divide by 0" << endl;
    }
Last edited on
Topic archived. No new replies allowed.