Integer to String
How can i turn an integer into a string? Like if int blah = 5, i want to be able to stick that 5 into a string and not turn it into a char. Anyone understand what im saying and know how to do this?
What programming language are you using? Assuming C++, I would just use boost::lexical_cast. You can achieve the same effect manually with a std::stringstream. In C you''d probably use sprintf().
int blah = 5;
string s;
s = blah;
Search the forum or google. Questions like this come up around every 5 minutes.
string s;
s = blah;
Search the forum or google. Questions like this come up around every 5 minutes.
quote:Original post by Mathematix
This is not a game, so it must be homework.
How do you figure? I haven''t played many games that didn''t use strings in one form or another.
I need to pass it to a function though as a char ><. And mathematix, i dont take a programming course
quote:Original post by JoHnOnIzEr
I need to pass it to a function though as a char ><. And mathematix, i dont take a programming course
Again, google. I can''t see it taking you more than 5 straight seconds to find an answer to this.
Look up:
string::c_str()
// needed stl headers#pragma warning(disable:4786)#include <sstream>#include <string>// variablesstd::stringstream ss;std::string MyString;int blah = 5;// convert from int to stringss << blah;ss >> MyString;// get char* arraychar const* pszMyString = MyString.c_str();
quote:Original post by glassJAw
int blah = 5;
string s;
s = blah;
This doesn''t do what you think it does. This creates a string where the first character is equal to a non-printing ascii character. s will not equal "5".
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement