I’m trying to convert the string to int with stringstream, the code down below works, but if i use a number more then 1234567890, like 12345678901 then it return 0 back …i dont know how to fix that, please help me out
std:: string number= "1234567890";
int Result;//number which will contain the result
std::stringstream convert(number.c_str()); // stringstream used for the conversion initialized with the contents of Text
if ( !(convert >> Result) )//give the value to Result using the characters in the string
Result = 0;
printf ("%d\n", Result);
Hm, lots of disinformation in the existing four or five answers.
An
intis minimum 16 bits, and with common desktop system compilers it’s usually 32 bits (in all Windows version) or 64 bits. With 32 bits it has maximum 232 distinct values, which, setting K=210 = 1024, is 4·K3, i.e. roughly 4 billion. Your nearest calculator or Python prompt can tell you the exact value.A
longis minimum 32 bits, but that doesn’t help you for the current problem, because in all extant Windows variants, including 64-bit Windows,longis 32 bits…So, for better range than
int, uselong long. It’s minimum 64 bits, and in practice, as of 2012 it’s 64 bits with all compilers. Or, just use adouble, which, although not an integer type, with the most common implementation (IEEE 754 64-bit) can represent integer values exactly with, as I recall, about 51 or 52 bits – look it up if you want exact number of bits.Anyway, remember to check the stream for conversion failure, which you can do by
s.fail()or simply!s(which is equivalent tofail(), more precisely, the stream’s explicit conversion toboolreturns!fail()).