"clear" an ostringstream

W

wang

Hi all,

I tried to format several numbers into strings using an ostringstream
in the following way:

ostringstream oss;
double x = 1.2, y = 5.3;
string xs, ys;
oss << x;
xs = oss.str();
oss << y;
ys = oss.str();

But ys has the result "1.25.3". Obviously y is appended to x. oss must
be "cleared" before y is formatted. But how? I inserted "oss.str("");"
before "oss << y;", the result is the same -- the empty string is
appended to oss again. Thanks for any help!
kwwang
 
I

Ian Collins

Hi all,

I tried to format several numbers into strings using an ostringstream
in the following way:

ostringstream oss;
double x = 1.2, y = 5.3;
string xs, ys;
oss<< x;
xs = oss.str();
oss<< y;
ys = oss.str();

But ys has the result "1.25.3". Obviously y is appended to x. oss must
be "cleared" before y is formatted. But how? I inserted "oss.str("");"
before "oss<< y;", the result is the same -- the empty string is
appended to oss again.

You could just use a stringstream:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::stringstream ss;
double x = 1.2, y = 5.3;
std::string xs, ys;

ss << x << ' ' << y;

ss >> xs >> ys;

std::cout << xs << ' ' << ys << std::endl;
}
 
F

Francesco S. Carta

Hi all,

I tried to format several numbers into strings using an ostringstream
in the following way:

ostringstream oss;
double x = 1.2, y = 5.3;
string xs, ys;
oss<< x;
xs = oss.str();
oss<< y;
ys = oss.str();

But ys has the result "1.25.3". Obviously y is appended to x. oss must
be "cleared" before y is formatted. But how? I inserted "oss.str("");"
before "oss<< y;", the result is the same -- the empty string is
appended to oss again. Thanks for any help!

The best advice I could give is to follow FAQ 5.8, in particular by
posting complete code.

I suspect that you've also messed up your description, because the
solution you tried should have worked properly.
 
W

wang

Try:

oss.str() = "";

or:

oss.str(std::string());

--
--------------------------------- --- -- -
Posted with NewsLeecher v4.0 Beta 20
Web @http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Thank you! the following code meets my need:

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
double x = 1.2, y = 6.5;
string xs, ys;
ostringstream oss;
oss << x;
xs = oss.str();
oss.str(string());
oss << y;
ys = oss.str();
cout << xs << endl;
cout << ys << endl;
return 0;
}

kwwang
 
F

Francesco S. Carta

This is my first post on C++, apart from my question above. Little did I know that it's an iniquitous den of pedantic nerdelwerfors. I have learnt my lesson and will leave you to your obfuscations, which will probably involve some form of C++ template meta-
programming.

Nobody is telling you to leave, you might very well stay, confront and
learn, instead of taking the first reply as pedantic and overreact.

You have pointed out 50% of non-working solution, I had to point out
that to the OP, somehow. If you got offended by those small ironical
posts of mine - and I guarantee you that I'm one of the kindest persons
here - you would be better strengthening your sensibility a bit.
 
F

Francesco S. Carta

Thank you! the following code meets my need:

#include<string>
#include<sstream>
#include<iostream>
using namespace std;
int main() {
double x = 1.2, y = 6.5;
string xs, ys;
ostringstream oss;
oss<< x;
xs = oss.str();
oss.str(string());
oss<< y;
ys = oss.str();
cout<< xs<< endl;
cout<< ys<< endl;
return 0;
}

Just for the records, notice that your original solution would have
worked fine - quoting your words from the original post:
I inserted "oss.str("");" before "oss << y;",

oss.str("");

and

oss.str(string());

achieve just the same result.

Hence there must have been some other problem hanging around when you
posted your original question.
 
B

Bo Persson

wang said:
I'm to 99% certain that you are right. Neither oss.str("") nor
oss.str(string()) works with MS VC++6. On Ubuntu Linux with g++,
oss.str(string()) works just fine. I've forgotten if I've tested
with oss.str("") on Linux.
Since I realized that VC++6 doesn't work with ostringstream this
way, I went back to ANSI C and used sprintf() to fulfil my task. I
wanted to write "pure" C++ code, but VC++6 doesn't let me do it.
kwwang

You have just identified one of the biggest problems in writing "pure"
C++ code: VC6.

That is a release published 12 years ago, just before the C++
standard. A decent compiler during the previous millennium, but you
shouldn't be using that now unless soemone is paying you big $$$ for
supporting equally old software.

Current version is VC10.


Bo Persson
 
W

wang

I'm to 99% certain that you are right. Neither oss.str("") nor
oss.str(string()) works with MS VC++6. On Ubuntu Linux with g++,
oss.str(string()) works just fine. I've forgotten if I've tested with
oss.str("") on Linux.
Since I realized that VC++6 doesn't work with ostringstream this way,
I went back to ANSI C and used sprintf() to fulfil my task. I wanted
to write "pure" C++ code, but VC++6 doesn't let me do it.
kwwang- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

On Ubuntu Linux with g++, oss.str("") works fine, too.
kwwang
 
J

James Kanze

I tried to format several numbers into strings using an ostringstream
in the following way:
ostringstream oss;
double x = 1.2, y = 5.3;
string xs, ys;
oss << x;
xs = oss.str();
oss << y;
ys = oss.str();
But ys has the result "1.25.3". Obviously y is appended to x.
oss must be "cleared" before y is formatted. But how?
I inserted "oss.str("");" before "oss << y;", the result is
the same -- the empty string is appended to oss again.

As others have pointed out, inserting the oss.str("") should
work. Still, I'm surprised that no one pointed out the obvious
(and cleanest) solution: just use a new ostringstream each time.
The surest way of getting a "cleared" object (of any type) is to
construct a new one. `oss.str("")' only clears the string's
contents, for example; it doesn't reset any formatting flags or
whatever that might have been set.
 
W

wang

As others have pointed out, inserting the oss.str("") should
work. Still, I'm surprised that no one pointed out the obvious
(and cleanest) solution: just use a new ostringstream each time.
The surest way of getting a "cleared" object (of any type) is to
construct a new one.  `oss.str("")' only clears the string's
contents, for example; it doesn't reset any formatting flags or
whatever that might have been set.

Yes, I came to this idea, too. I wrote a subprogramm (or a private
method?) to do the task, and the ostringstream is defined in this
subprogramm. This way I avoided using sprintf().
kwwang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top