How to convert int to string in C++?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Please Disable the AdBlocker to Continue to the site.
Integer variables can be converted to strings using one of two methods. The two methods are described below along with a sample of the code.
Approach-1
#include<iostream>
#include<string>
using namespace std;
void main()
{
int n= 1;
string s= to_string(n);
cout << s;
}
Approach-2
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int n = 17;
// declaring output string stream
ostringstream s1;
// Sending a number as a stream into output str
s<< n;
// the str() converts number into string
string fin = s.str();
// Displaying the string
cout << fin;
return 0;
}