+ 4

C++ int to string code

I'm writing a program where i will need to convert an int variable into a string type.. finding it abit hard can someone please send me a c++ code for this.. will be much appreciated thanks

20th Mar 2018, 1:36 PM
SedemQuame
SedemQuame - avatar
4 Réponses
+ 5
If you're using the C++11 standard or newer (Code Playground doesn't), just #include <string> and use to_string(): int a = 10; String str = to_string(num); Otherwise you can #include <sstream> and work with a stringstream: int a = 10; stringstream ss; ss << a; string str = ss.str(); If you want to reuse the stringstream, write the following to empty it: ss.str(string()); // string() is an empty string
20th Mar 2018, 1:41 PM
Chris
Chris - avatar
+ 9
Actually, Code Playground supports to_string now. https://code.sololearn.com/c2Ayjh3HgWfp/?ref=app Check this thread by @Swim. https://www.sololearn.com/Discuss/1145740/?ref=app
20th Mar 2018, 2:58 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
If you don't want to use library code or you own wish to use strings that you process, this code provides both options. I coded it a while ago, but got interrupted so I didn't post it here. https://code.sololearn.com/c5W0c6ExkmP4
20th Mar 2018, 4:27 PM
John Wells
John Wells - avatar
0
hey there my friend use should use string header file which is normally not a standard c++ header file but from c++ 11th standard all compilers have it to covert into to string u need to use to_string() function eg: #include<string> int n; void main() { cin>>n; String n=to_string(n); } but u should know some bit about string class u can't use char array since to_string() return String type which is a class in string header file sorry for a little big answer
20th Mar 2018, 2:14 PM
Nivas Muthu
Nivas Muthu - avatar