Thread: help with loop condition

  1. #1
    Unregistered
    Guest

    help with loop condition

    can someone help me, this little demo program (to convert string to stream) seems to be stuck in the while loop

    can someone give me a suggestion

    objective to loop though a string of varying lengths and separate out the words by the commas

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include "sstream.h"
    
    int main()
    {
    
         string s="John Jones, Day, Night,";
         string temp;
         istringstream iss(s);
    
         while(true)
         {
          getline(iss, temp,',');
          if(temp.size()==0) break;
          cout << temp <<" ";
         }
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Im not sure because I dont use those functions, but an infinite loop could be caused if the getline function doesnt set the string 'temp' to "" when there is on more information in the stream, i.e. temp retains the value from the previous call. A possible remedy would be to test whether the stream itself is still valid. How about trying iss.eof(), iss.fail(), iss.good(), !iss -- whichever one works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM