PDA

View Full Version : Istringstream gives me wrong result for a signed short int



tonnot
13th May 2011, 19:18
I have :

signed short int my_i // -32768 to 32767

std::istringstream i(value);
i>>my_i

Ok, if I pass value "38000" my_i gives me -27536
Ok, if I pass value "-27536" my_i gives me -27536

So the instruction to know if can be posible to convert to My_i does not fails.
if (!(i >> My_i) )
for the value "38000" 'i' have not to convert, it should to fail, but it works ... converting 38000 to -27536 )


In this situation I can't have the correct value.
Any help ? Maybe mingw the guilty ?
Thanks

squidge
13th May 2011, 20:32
I don't see why the code i>>my_i would ever return zero?

tonnot
13th May 2011, 20:43
If I have for std::istringstream i(value); a value of 450000
(i >> My_i) are going to be false, because istringstream cannot 'send' a long value to a signed short int, isn't ?
Ok, then why Is the reason because 38000 , value that also would have to be converted to long, is converted to -27536 ?
Thanks

Added after 4 minutes:

Uf , delete this post please
I have mixed int and short int inside functions ....

SixDegrees
14th May 2011, 15:08
You don't understand how stringstreams work. Stop making assumptions and go read some documentation (http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/).

And your test is silly to begin with. You can't stuff an 'illegal' value into a variable that's been declared to be of a particular type already; whatever value it holds will ALWAYS be legitimate and within the range of values allowed by the type. There's no point in performing the check you're attempting in the way you're trying to go about it.