How to convert int to string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to convert int to string?

how to convert the value of int to string? I already convert the string to int to make a calculation. After the calculation how to change the answer back to string? Here is the coding that i already write to convert the string to int. #include <iostream> #include <string> #include <cstdlib> #include <sstream> using namespace std; int main() { string x,z; int y,a,sum; cout<<"input the number x"<<endl; cin>>x; cout<<"input the number z"<<endl; cin>>z; a = stoi(z); y = stoi(x); cout<<"the number of y : "<<y<<endl; cout<<"the number of z : "<<z<<endl; sum=y+a; cout<<"sum = "<<sum<<endl; return 0; } now i need the solution to convert the (int sum) to string?

1st Nov 2017, 8:01 AM
Dennis Goh
Dennis Goh - avatar
3 Answers
+ 18
#include <sstream> int main() { std::stringstream obj; std::string str; int value = 3939; obj << 3939; obj >> str; }
1st Nov 2017, 8:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
In C++11 and later, you may even use 'to_string()' for the same. Eg - string res = to_string(2);
1st Nov 2017, 10:11 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Thank you very much!!
1st Nov 2017, 8:22 AM
Dennis Goh
Dennis Goh - avatar