Thread: Flush / Empty a string

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Flush / Empty a string

    Hi

    I am new to this!

    how can I empty a string to be able to use it again in a loop?

    Code:
    stringstream myString;
    
    while (something) {
    
    // putting things into myString
    
    cout << myString.str(); // print it out
    
    // here I want to flush the string so it is empty at the beginning of the loop
    
    // this does not work:
    myString.str() = "";
    // nor does this:
    myString.flush(); 
    
    }

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Maybe try setting it equal to \0.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    is there not a string.empty() function or you could just put string = ""

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    myString.str("");
    Kurt

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    7

    thanks!

    Thanks ZuK (Kurt)!



    Re-declare it in the beginning of the loop also worked.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unless you need it outside the loop, declaring it (not re-declaring it) inside the loop is probably the best solution.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Damn. I thought that I could actually help some one for once, but every one beat me to it.
    I only learned this a few days ago, but I use :

    Code:
    myString.str("");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM