0

I am trying to write a bool function that looks at the first index in an array which contains a positive or negative number and classifies if it is a negative sign (i.e. -). If it is a negative Sign it returns false everything else returns true. I am trying to figure out how to compare the negative sign. The following code give an error because of the '-'

    bool BigNum::get_positive() const
{
char '-';
if(digits[0] == '-')
{
    return false;
}
else
{
    return true;
}
}
4
  • 1
    what do you expect to achieve with this: char '-';?
    – Nim
    Sep 21, 2011 at 7:47
  • 1
    There are some existing libraries for big integers [I assume that is what you are trying to do], why reinvent the wheel?
    – amit
    Sep 21, 2011 at 7:53
  • Also, this is more of a style issue, but since your function is trying to determine whether an instance of BigNum represents a positive number, why not call it isPositive()? A function called get_X() usually suggests that it's returning some stored info, which is not the case here.
    – Anson
    Sep 21, 2011 at 8:02
  • An additional style issue is the if (condition) return true; else return false;, which should logically be return !condition; (without the if). Sep 21, 2011 at 8:12

3 Answers 3

5
char '-';

The compiler thinks you're trying to declare a char, but that's not a valid declaration.

Your entire function could be replaced with:

return (digits[0] != '-');

Of course, this is assuming that [0] is a valid index of digits. If not, bad things will happen. If you know the length of the array, you can do a check like this:

if( digits_length < 1 )
  return false;
return (digits[0] != '-');
4
  • 1
    Wouldn't this better be written return digits_length > 0 && digits[0] != '-'? (Supposing you want a result in this case: I suspect that throwing an exception, or even aborting, would be more appropriate.) Sep 21, 2011 at 8:11
  • @James: well, if the string is 0 length then it doesn't start with a -, but it also doesn't represent a number. So it sort of depends how the function is intended to be used. Sep 21, 2011 at 8:51
  • @Steve Yes. As often occurs here, we're asked the best implementation of a function without being told what it should do. Since there was some talk of "negative" (which only makes sense on a number), I assumed that this was a precondition, but the original poster really didn't specify. Returning a value (an arbitrary bool, or an enum), throwing an exception, or aborting are all valid responses, depending on the exact specifications of the function. Sep 21, 2011 at 9:24
  • @James: Agreed. Even UB might be reasonable, if as you say it's a precondition that the input be a valid representation of a number. I think I can generalize, people who can fully specify this function would know how to write it (a version that works, I mean, not necessarily agree on the most readable). An expert programmer with absolutely zero knowledge of C syntax might be an exception, but an expert would read a bit rather than ask this sort of question. Sep 21, 2011 at 9:34
1

you must delete or comment "char '-';"

1

Mistake lies in line char '-'. '-' is supposed to be stored in some variable which later could be used in if clause to compare. This is a syntactical error because you havn't defined a storage for '-'.

Otherwise as pointed above just delete this line and get away with using '-' in if (as you have already done it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.