stringstream clear

This is a discussion on stringstream clear within the C++ Programming forums, part of the General Programming Boards category; I've read on several websites that in order to use stringstream more than once I'm supposed to use stringstream ss; ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    stringstream clear

    I've read on several websites that in order to use stringstream more than once I'm supposed to use
    stringstream ss;
    ss.str("");

    PHP Code:
    http://www.gamedev.net/community/forums/viewreply.asp?ID=516546 
    though, it doesn't seem to work for me

    Code:
    stringstream streamstr;
    streamstr << "TM" << GalilInternalClock;
    string sendstring;
    streamstr >> sendstring;
    		
    streamstr.str(""); //suuposed to clear streamstr
    streamstr << "DR" << GalilDataRecordFrequency;
    streamstr >> sendstring; 
    
    //and sendstring is still "TM1000" for some reason!!!
    What am I doing wrong?
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,617
    hmm... I am not sure why that did not work, though I would suggest:
    Code:
    streamstr.clear();
    instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,785
    In this case, both .str("") and .clear() are needed. The reason is that the eofbit is set after the first extraction.

    If all you need is the stream's string, then you can get it without using an extraction method with "sendstring = streamstr.str();". If you never use any "extractors" then the eofbit will never be set - then you can "reset" the stream with just a .str("").

    gg

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,617
    In this case, both .str("") and .clear() are needed. The reason is that the eofbit is set after the first extraction.
    Ah yes, I was trying to remember why it is needed here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About clear(). Please help!
    By Antigloss in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2005, 04:02 AM
  2. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  3. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 09:40 PM
  4. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 04:14 AM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21