+ 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?
3 Respuestas
+ 18
#include <sstream>
int main()
{
std::stringstream obj;
std::string str;
int value = 3939;
obj << 3939;
obj >> str;
}
+ 3
In C++11 and later, you may even use 'to_string()' for the same.
Eg - string res = to_string(2);
+ 2
Thank you very much!!