Thread: I need to loop getline forever, but only let getline block when necessary. Solution?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Gainesville, Florida, United States
    Posts
    2

    I need to loop getline forever, but only let getline block when necessary. Solution?

    Hello!

    So my problem is this, my program has a getline(cin, stringvar) within a while(true) loop and I want to do other tasks if getline doesn't have any input to read from cin, instead of just sitting there and blocking.

    Any ideas?

    Code:
    void my_other_task()
    {
    cout << "I am so busy" << endl;
    } bool is_there_input() {
    //What goes here?
    } int main() {
    while(true) {
    string user_input; if(is_there_input())
    getline(cin,user_input);
    else
    my_other_task();
    }
    }
    my first idea was to just run 2 threads, one repeating 'my_other_task()' over and over, and the other repeating getline(..), but I think this solution would be better in my case, if I could figure out how to solve this.

    Any ideas on the correct way to do this?
    (my initial guess was that since cin is an 'istream', I could use peek(), but it doesn't seem to work...)

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    Gainesville, Florida, United States
    Posts
    2
    By the way, this is what I tried when I attempted to use cin.peek(), with no success. getline still manages to block.

    Code:
    while(true)
    {
                   bool cinready = cin.peek() == EOF ? false : true;
                      if(cinready)
                      {
                       getline(cin , command);
                  }
                  else
                  {
                      check_job_status();
                      continue;
                  }
    
    handle_command(command);
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    checking the character you read for EOF is not the way to find the end of input. it's unlikely that your user will EVER enter an EOF character, so it's pointless to check for it. if you really want non-blocking behavior at the console, you can use select() or poll() to check if there is input waiting on the stdin file descriptor if they are supported on your platform, and read input directly from the file descriptor using read(). aside from that, I don't know of any way to do non-blocking reads from stdin with the C++ standard library.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    maybe this helps: you can use std::istream::readsome() to get data from an istream object. this particular function will return only data that is already in the buffer. it will not block to wait for more, and if there is no data in the buffer, it simply returns without putting anything in your buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I stop my loop from skipping getline
    By Darkroman in forum C++ Programming
    Replies: 2
    Last Post: 08-29-2011, 02:09 PM
  2. problems with getline in a while loop
    By Crall13 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2005, 01:32 PM
  3. cin.getline and cin problems in a while loop
    By UnclePunker in forum C++ Programming
    Replies: 12
    Last Post: 05-07-2002, 09:43 AM
  4. help with infile and getline using for loop
    By lostgirls in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2002, 02:36 PM
  5. getline overload, solution didn't work
    By Clane in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2002, 09:53 AM