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?
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 }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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).