Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "clear" an ostringstream

Reply
Thread Tools

"clear" an ostringstream

 
 
wang
Guest
Posts: n/a
 
      08-14-2010
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
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-15-2010
Artful <(E-Mail Removed)>, on 14/08/2010 19:02:23, wrote:

> Try:
>
> oss.str() = "";
>
> or:
>
> oss.str(std::string());


"Try" stands for "they both work" or for "I don't recall which one works"?

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-15-2010
On 08/15/10 12:10 PM, Francesco S. Carta wrote:
> Artful <(E-Mail Removed)>, on 14/08/2010 19:02:23, wrote:
>
>> Try:
>>
>> oss.str() = "";
>>
>> or:
>>
>> oss.str(std::string());

>
> "Try" stands for "they both work" or for "I don't recall which one works"?


Well one works!

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-15-2010
On 08/15/10 10:50 AM, wang wrote:
> 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;
}

--
Ian Collins
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-15-2010
Ian Collins <(E-Mail Removed)>, on 15/08/2010 12:14:19, wrote:

> On 08/15/10 12:10 PM, Francesco S. Carta wrote:
>> Artful <(E-Mail Removed)>, on 14/08/2010 19:02:23, wrote:
>>
>>> Try:
>>>
>>> oss.str() = "";
>>>
>>> or:
>>>
>>> oss.str(std::string());

>>
>> "Try" stands for "they both work" or for "I don't recall which one
>> works"?

>
> Well one works!


Indeed.

Unfortunately I slipped on the chance to reply:

"Hey, you miss a 'catch:' up there!"

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-15-2010
wang <(E-Mail Removed)>, on 14/08/2010 15:50:42, wrote:

> 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.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
wang
Guest
Posts: n/a
 
      08-15-2010
On 15 Aug., 02:02, Artful <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-15-2010
Artful <(E-Mail Removed)>, on 14/08/2010 22:09:19, wrote:

> 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.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-15-2010
wang <(E-Mail Removed)>, on 14/08/2010 22:37:24, wrote:

> On 15 Aug., 02:02, Artful<(E-Mail Removed)> wrote:
>> 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;
> }


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.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      08-16-2010
wang wrote:
> On 15 Aug., 22:23, "Francesco S. Carta" <(E-Mail Removed)> wrote:
>>
>> Hence there must have been some other problem hanging around when
>> you posted your original question.

>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: using ZLIB to compress std::ostringstream? Mike C++ 1 08-03-2003 06:50 PM
Re: ? How to clear ostringstream buffer Mark Warren C++ 3 07-23-2003 10:12 AM
vector of ostringstream Alex Vinokur C++ 4 07-14-2003 02:36 AM
Re: std::ostringstream unexpected behavior with .net 2003. Russell Hanneken C++ 0 06-25-2003 10:22 PM
Re: std::ostringstream unexpected behavior with .net 2003. Victor Bazarov C++ 0 06-25-2003 10:20 PM



Advertisments