[try Beta version]
Not logged in

 
How to make error messages?

Mar 27, 2009 at 1:31am
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 Mar 27, 2009 at 9:35pm
Mar 27, 2009 at 1:54am
1
2
3
4
if(value < 0)
{
  cout << "Error: Negative Number entered\n";
}
Mar 27, 2009 at 2:54am
Throw an exception?
Mar 27, 2009 at 3:35am
Why not make the function take an unsigned and avoid the problem in the first place?
Mar 27, 2009 at 7:14am
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.
Mar 27, 2009 at 12:37pm
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.
Mar 27, 2009 at 7:10pm
Oh yes; it wouldn't implicitly cast to an unsigned... Thanks.
Topic archived. No new replies allowed.