How to make error messages?

Lets say I made a square root function and I want an error message if the input is negative. How...

EDIT: This is just an example. I want to make errors for other things as well. I'm asking if there is a specific way to test for errors and then output an error message instead of returning a value.
Last edited on
1
2
3
4
if(value < 0)
{
  cout << "Error: Negative Number entered\n";
}
Throw an exception?
Why not make the function take an unsigned and avoid the problem in the first place?
jsmith, would that still apply if the input was determined at run-time? I am under the impression that using unsigneds would only give compiler errors if a literal (or a value available at compile-time) was used.
If the function is declared as

unsigned sqrt( unsigned );

then passing any constant or variable into the function will generate a compile error if the actual parameter is signed.
Oh yes; it wouldn't implicitly cast to an unsigned... Thanks.
Topic archived. No new replies allowed.