"clear" an ostringstream

Discussion in 'C++' started by wang, Aug 14, 2010.

  1. wang

    wang Guest

    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
    wang, Aug 14, 2010
    #1
    1. Advertising

  2. Francesco S. Carta, Aug 15, 2010
    #2
    1. Advertising

  3. wang

    Ian Collins Guest

    On 08/15/10 12:10 PM, Francesco S. Carta wrote:
    > Artful <>, 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
    Ian Collins, Aug 15, 2010
    #3
  4. wang

    Ian Collins Guest

    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
    Ian Collins, Aug 15, 2010
    #4
  5. Ian Collins <>, on 15/08/2010 12:14:19, wrote:

    > On 08/15/10 12:10 PM, Francesco S. Carta wrote:
    >> Artful <>, 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
    Francesco S. Carta, Aug 15, 2010
    #5
  6. wang <>, 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
    Francesco S. Carta, Aug 15, 2010
    #6
  7. wang

    wang Guest

    On 15 Aug., 02:02, Artful <> 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
    wang, Aug 15, 2010
    #7
  8. Artful <>, 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
    Francesco S. Carta, Aug 15, 2010
    #8
  9. wang <>, on 14/08/2010 22:37:24, wrote:

    > On 15 Aug., 02:02, Artful<> 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
    Francesco S. Carta, Aug 15, 2010
    #9
  10. wang

    Bo Persson Guest

    wang wrote:
    > On 15 Aug., 22:23, "Francesco S. Carta" <> 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
    Bo Persson, Aug 16, 2010
    #10
  11. wang

    wang Guest

    On 16 Aug., 17:09, wang <> wrote:
    > On 15 Aug., 22:23, "Francesco S. Carta" <> wrote:
    >
    >
    >
    >
    >
    > > > #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.

    >
    > 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
    wang, Aug 17, 2010
    #11
  12. wang

    James Kanze Guest

    On Aug 14, 11:50 pm, wang <> wrote:

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

    --
    James Kanze
    James Kanze, Aug 17, 2010
    #12
  13. wang

    wang Guest

    On 17 Aug., 12:20, James Kanze <> wrote:
    > On Aug 14, 11:50 pm, wang <> wrote:
    >
    > > 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.
    >
    > --
    > James Kanze


    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
    wang, Aug 18, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    846
    Victor Bazarov
    Jun 25, 2003
  2. Mark Warren
    Replies:
    3
    Views:
    16,546
    Sam Holden
    Jul 23, 2003
  3. Kyle Kolander

    ostringstream clear

    Kyle Kolander, Jul 3, 2005, in forum: C++
    Replies:
    3
    Views:
    669
    Steven T. Hatton
    Jul 5, 2005
  4. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,018
    Mark Fitzpatrick
    Jan 31, 2008
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    957
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page