I'm new to programming so sorry if this is an easy question. But how do I change and int variable to a string variable?
Printable View
I'm new to programming so sorry if this is an easy question. But how do I change and int variable to a string variable?
Use a stringstream:
Code:#include <string>
#include <sstream>
#include <iostream>
int main()
{
int value = 100;
std::stringstream sstr;
sstr << value; // Store integer in stringstream
std::string str = sstr.str(); // Convert stringstream to string
std::cout << str << std::endl; // Output string
}
Also you can use itoa(); Im not sure but i think itoa() might be windows specific so it might be a better option to use stringstream.
Code:
string buffer;
int number = 0;
itoa(number,buffer,10);
I think you're thinking of atoi(), which is standard (stdlib), does the opposite, and wouldn't work with STL strings even if it did exist (unless of course windows.h includes this function, but I doubt it).