I'm doing an assignment, the assignment has us taking a string and converting it to a integer or double. We had to make a function that checks to make sure the number is valid, and I can't for the life of my figure out why mine never changes whether its valid or not.
Now the value of NaN only depends on the last character in the string. You want NaN to false only if all characters are digits.
But that doesn't explain why NaN is not false. You could print the string variables a and b to make sure they has the correct content. They should not contain any additional characters (not even spaces).
They both hold exactly whats entered, someone else mentioned that isdigit uses characters where as I'm passing it a string, and that may be whats wrong.
However I'm not quite sure how to pass it a character reference so I could use a for loop to iterate through to make sure every character is a number.
And the decimal point in the input string, how will that go with std::isdigit ?
Would it not be better to see whether the stringstream fails or not? If it does, then it's not a double. Then it will accept doubles like +1.23e+2, and reduce your code :+)
if the input is 1.23 , the decimal point isn't a digit, is it?
The second part:
std::stringstream will fail if it's input is not a valid double. SO I am saying it's better to test that near line 50 of the class cpp file. Then you won't need Double::isDigit at all.