+ 1

Why doesn't that work?

std::string Contostr(int num){ std::stringstream ss; ss << num; ss.str(); return num; } And is there any other ways to do so?

23rd Aug 2018, 10:44 AM
Oleg Storm
Oleg Storm - avatar
7 Antworten
+ 3
KarSah, i have already managed the problem: std::string Contostr(int num){ std::string str; std::stringstream ss; ss << num; ss >> str; return str; }
23rd Aug 2018, 11:19 AM
Oleg Storm
Oleg Storm - avatar
+ 1
I am not sure,but i think its becouse your function must return std::string but you are returning int
23rd Aug 2018, 11:10 AM
KarSah
KarSah - avatar
+ 1
KarSah, but doesn't num converts to string in this line: ss.str(), as i added num here: ss<<num;?
23rd Aug 2018, 11:12 AM
Oleg Storm
Oleg Storm - avatar
+ 1
Oleg Storm,i think no, cause num is another variable,if i understand correctly you need to return ss
23rd Aug 2018, 11:18 AM
KarSah
KarSah - avatar
+ 1
Anyway, thanks for your help🤗
23rd Aug 2018, 11:20 AM
Oleg Storm
Oleg Storm - avatar
+ 1
Oleg Storm,no problem😊,but you can make it without another string variable,simply returning ss.str() std::string Contostr(int num){ std::stringstream ss; ss << num; return ss.str(); }
23rd Aug 2018, 11:29 AM
KarSah
KarSah - avatar
0
Oleg Storm , other option to achieve same is to_string...but prefer stringstream approach... might like to check below : https://code.sololearn.com/c2l7g4m0zmas/?ref=app
23rd Aug 2018, 1:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar