+ 2

c++ how i can combine int and string ?

how can i combine string and int for example int x = 30; string name ="john"; string result = name + x; cout <<result<<endl;

9th Aug 2017, 1:45 AM
John Nehry C. Dedoro
John Nehry C. Dedoro - avatar
1 Réponse
+ 12
Erm, guys. No operator "+" matches std::string + int. You have to explicitly convert int to string and then concatenate it using +. #include <iostream> #include <sstream> #include <string> int main() { std::stringstream obj; std::string test = "test"; int integer = 39; obj << integer; std::string test2 = test + obj.str(); std::cout << test2; return 0; }
9th Aug 2017, 3:23 AM
Hatsy Rei
Hatsy Rei - avatar