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

  2. "Try" stands for "they both work" or for "I don't recall which one works"?
     
    Francesco S. Carta, Aug 15, 2010
    #2
    1. Advertisements

  3. wang

    Ian Collins Guest

    Well one works!
     
    Ian Collins, Aug 15, 2010
    #3
  4. wang

    Ian Collins Guest

    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, Aug 15, 2010
    #4
  5. Indeed.

    Unfortunately I slipped on the chance to reply:

    "Hey, you miss a 'catch:' up there!"
     
    Francesco S. Carta, Aug 15, 2010
    #5
  6. 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.
     
    Francesco S. Carta, Aug 15, 2010
    #6
  7. wang

    wang Guest

    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. 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.
     
    Francesco S. Carta, Aug 15, 2010
    #8
  9. Just for the records, notice that your original solution would have
    worked fine - quoting your words from the original post:
    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.
     
    Francesco S. Carta, Aug 15, 2010
    #9
  10. wang

    Bo Persson Guest

    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 Ubuntu Linux with g++, oss.str("") works fine, too.
    kwwang
     
    wang, Aug 17, 2010
    #11
  12. wang

    James Kanze Guest

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

    wang Guest

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

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 (here). After that, you can post your question and our members will help you out.