You can make a simple function to do this using the properties of logarithms.
using namespace std;
void IntToString(int i, string & s)
s = "";
if (i == 0)
s = "0";
if (i < 0)
s += '-';
i = -i;
int count = log10(i);
while (count >= 0)
s += ('0' + i/pow(10.0, count));
i -= static_cast<int>(i/pow(10.0,count)) * static_cast<int>(pow(10.0,count));
int i = -1024;
string s = "";
cout << "The string is now: " << s << endl;
I have found an example of sprintf() that look like this:
"For example, the following code uses sprintf() to convert an integer into a string of characters"
int num = 24;
sprintf( result, "%d", num );
Actually I dont understand the logic of this. I understand that int num = 24. This is the number. But what is char result exactly and where does the output go to a std::string ?
It look like something is missing.
Write yourself a little program to test your hypothesis. It's good practice and a good learning experience.
Alternatives to using a stringstream would be sprintf(), which is C style, or itoa() which is nonstandard.
char result; is what is called a C string, sprint f reads into the C string from number, which formats it as a string. If you don't want to use C strings, you can always use my solution that I posted =p