int to string problem

Hi Guys ,
I'm just working In a project and I need to convert integer value to a string variable < I've just tried all the functions that I know , and they all doesn't work , Please Help me in this ..
here is one of the codes that I've tried :
1
2
3
4
5
6
int num;
string res;
stringstream convert;
cin>>num;
convert << num;
res= convert.str();

it gives this error :
error C2228: left of '.str' must have class/struct/union
and this warning :
warning C4552: '<<' : operator has no effect; expected operator with side-effect

I've tried "itoa" function and same thing..
Please Help :)
Last edited on
Have a look at std::to_string()

http://www.cplusplus.com/reference/string/to_string/

Also have a look at the Boost.Lexical_Cast library

http://www.boost.org/doc/libs/1_57_0/doc/html/boost_lexical_cast.html
Last edited on
Thanks
but what's the library of "to_string" function ?
it doesn't recognize it !
'to_string': identifier not found
You are probably using a pre C++ 11 version of MSVC.
It's Pro Edition 2008 of MSVC .
The to_string() function will require a newer version of your compiler, I suggest the latest version.

Your stringstream method should work properly, as long as you use the correct variable names. Remember C++ is case sensitive, Num is not the same as num. Something more like:

1
2
3
4
5
6
7
8
9
10
int num;
cin>>num;

string res;
stringstream convert;

convert << num;
res= convert.str();

cout << res << endl;
Topic archived. No new replies allowed.