3

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?

Erk
  • 69
  • 1
  • 1
  • 7

6 Answers6

9

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);
Bdloul
  • 882
  • 10
  • 27
  • @JavaRunner, is there any other reason not to use `std::to_string`? As I now consider replacing `stringstreams` used for one value with `to_string`, but not sure, this is a good idea. – sukhmel Sep 26 '14 at 08:12
3

The stringstream is a variable just like any other. To make the idiom work twice without change, put it in braces:

int a = 13;
string str2;
{
    stringstream ss;
    ss << a;
    str2 = ss.str();
} // ss ceases to exist at the closing brace

int b = 15;
string str3;
{
    stringstream ss; // now we can make a new ss
    ss << b;
    str3 = ss.str();
}

Since C++11, you can do

std::string str4 = std::to_string( b );

or (with c of arbitrary type)

std::string str5 = ( std::stringstream() << c ).str();

There are other solutions, of course.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • thank you for the info. when I did it that way I got no errors however when I tried to use the strings outside of the brackets later in my code it said they weren't defined in this scope. I think I solved my problem though, I'm just going to use different names for each stringstream. – Erk Mar 13 '13 at 02:52
  • @user2162870 Note how I declared the strings here, outside the braces. Also, you might consider factoring it into a function. (You might even call the function `to_string` to simplify the eventual migration to C++11.) Copy-paste code is harder to maintain. – Potatoswatter Mar 13 '13 at 05:16
  • How does the `( std::stringstream() << b ).str()` idiom work? As far as I can see, `<<` returns an `ostream&`, which doesn't have `str()`. – Trygve Flathen Feb 11 '15 at 18:08
  • 1
    @TrygveFlathen Before C++11, `std::stringstream() << b` would take the stream as an `std::ostream &` lvalue reference which did not accept a temporary. When rvalue references were added to the language, a generic overload was added to the library, so I should update the answer. – Potatoswatter Feb 11 '15 at 23:38
1

try

#include <string>

int a = 10;
std::string s = std::to_string(a);
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • I've tried to_string and my compiler doesn't understand it. I get this error: Function 'to_string' could not be resolved. I made sure that i was also using #include and #include – Erk Mar 13 '13 at 02:43
  • what's your development environment? – d.moncada Mar 13 '13 at 02:45
  • I do have the #include statement. I'm using eclipse on macOS. – Erk Mar 13 '13 at 02:54
  • Check out this SO post: http://stackoverflow.com/questions/7905025/string-could-not-resolved-error-in-eclipse-for-c ...but it sounds like your path may be setup incorrectly. – d.moncada Mar 13 '13 at 02:55
1

The particular error you are getting is telling you that you can't declare two variables with the same name (ss in your case) within the same scope.

If you wanted to create a new stringstream you could call it something else.

But there are other ways to convert an int to a string.. you could use std::to_string().

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
0

Make sure you declare:

using namespace std;

Then insert the code:

int a = 10;
ostringstream ssa;
ssa << a;
string str = ssa.str();

int b = 13;
ostringstream ssb;
ssb << b;
string str2 = ssb.str();

int c = 15;
ostringstream ssc;
ssc << c;
string str3 = ssc.str();
Joe
  • 104
  • 1
  • 13
0

One of the simplest way i can think of doing it is:

string str = string(itoa(num));
4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41