M
Mike Copeland
I am using istringstream to convert data. For example,
int parseInt(string source, int start, int length)
{
int iResult;
if((start >= 0) && (start < source.length()))
{
istringstream myStream(source.substr(start, length));
myStream >> iResult;
}
return iResult;
}
where (hopefully) the parameters are valid. In my testing, I find that
if the parameters are NOT valid (start < 0 or > source.length()) or if
the data in source isn't numeric, the code either skips the conversion
(as it should) or the conversion fails. In those cases, the function's
return value is -858993460, which I gather is the uninitialized value of
the integer variable iResult.
That's okay to a degree, but I'd like to be able to determine that an
error has occurred - and test for it in the calling logic. I can't find
any definition value for this default value, so I don't the
value/constant to test for, etc.
Given that atoi returns 0 for such errors, and given that I'd like a
more robust way of detecting errors (0 could be a legitimate conversion
result), I'd like to know what better solutions are available. Please
advise. TIA
int parseInt(string source, int start, int length)
{
int iResult;
if((start >= 0) && (start < source.length()))
{
istringstream myStream(source.substr(start, length));
myStream >> iResult;
}
return iResult;
}
where (hopefully) the parameters are valid. In my testing, I find that
if the parameters are NOT valid (start < 0 or > source.length()) or if
the data in source isn't numeric, the code either skips the conversion
(as it should) or the conversion fails. In those cases, the function's
return value is -858993460, which I gather is the uninitialized value of
the integer variable iResult.
That's okay to a degree, but I'd like to be able to determine that an
error has occurred - and test for it in the calling logic. I can't find
any definition value for this default value, so I don't the
value/constant to test for, etc.
Given that atoi returns 0 for such errors, and given that I'd like a
more robust way of detecting errors (0 could be a legitimate conversion
result), I'd like to know what better solutions are available. Please
advise. TIA