Thread: Goofy getline

  1. #1
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362

    Goofy getline

    I know all about the problems cin>> can cause, but I thought getline solved that. With this program
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string word;
    
      while (getline(cin, word))
      {
        cout<<"Read -- "<< word <<endl;
      }
    
      cout<<"Enter one last word: "<<flush;
      getline(cin, word);
      cout<<"Read -- "<< word <<endl;
    }
    Everything in the loop reads just find, but when I hit ctrl+z to get out of the loop, the next call to getline is skipped. I've tried this in Borland 5.5 and Dev-C++ 4 with the same results. Using cin.ignore() doesn't have any effect, nor does reading one character in a loop until there's nothing left. I have to be doing something wrong, but I can't see what. Any ideas?
    *Cela*

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You need to clear the eof bit. cin.clear().

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>You need to do the C++ equivalent of
    Ah, so simple. I use eof to terminate loops so regularly I completely forgot it counts as an error later.
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM