istringstream Conversion Question

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
 
F

Francesco S. Carta

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

To ensure a correct distinction between valid and invalid input, the
best technique (IMO) is to step through the chars until you get a valid
character (a digit, for example), and only then try to extract from the
stream to the variable - eventually putting back the valid character you
have just found.

There are other considerations to make in this particular case
(intercepting eventual plus and minus signs) and in general (letters and
periods are valid characters for hexadecimals and floats respectively,
also the scientific notation needs special care).

If you reach the end of the stream without finding any meaningful
character, you're ensured the input is invalid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top