C++ converting an int to a string

int, string

Solution

You're trying to redeclare the same stringstream with the same name. You can modify your code like this in order to work :

 int b = 13;
stringstream ss2;
ss2 << a;
string str2 = ss2.str();

Or like this if you don't want to redeclare it :

int b = 13;
ss.str(""); // empty the stringstream
ss.clear();
ss << a;
string str2 = ss.str();

You can also use , which is much quicker :

int c = 42;
std::string s = std::to_string(c);

Problem

I know this is extremely basic but I'm very new to C++ and can't seem to find an answer. I'm simply trying to convert a few integers to strings. This method works: ``` int a = 10; stringstream ss; ss << a; string str = ss.str(); ``` but when I need to convert the second and third ones like this: ``` int b = 13; stringstream ss; ss << a; string str2 = ss.str(); int c = 15; stringstream ss; ss << b; string str3 = ss.str(); ``` I get this error: ``` 'std::stringstream ss' previously declared here ``` Do I need to somehow close stringstream? I've noticed that if I put them far away from each other in my code the compiler doesn't mind but that doesn't seem like something I should do. Does anyone have suggestions?

Original source

Related problems