title: C++ select() not waiting for timeout period
body: <p>I'm trying to use the select function to accept input but every 2 seconds do something else if the user hasn't entered anything.  The code below is waiting two seconds the first time select() is reached but once it prints the first "timed out" message it rapidly keep printing out "timed out" without waiting for 2 seconds, basically entering an infinite loop.  Anyone know what the problem is?  Thanks for any help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  using namespace std;
  
  const int STDIN = 0;
  int main(int argc, int *argv[])
  {
  struct timeval tv;
  fd_set readfds, master;
  tv.tv_sec = 2;
  tv.tv_usec = 0;
  FD_ZERO(&amp;readfds);
  FD_ZERO(&amp;master);
  FD_SET(STDIN, &amp;readfds);
  FD_SET(STDIN, &amp;master);
  string buffer = "";
  while(buffer != "quit"){
      readfds = master;
      if(select(STDIN+1, &amp;readfds, NULL, NULL, &amp;tv) == -1) perror("select");
      if (FD_ISSET(STDIN, &amp;readfds)){
          getline(cin, buffer);
          cout &lt;&lt; "You entered: " &lt;&lt; buffer &lt;&lt; endl;
      }else
          cout &lt;&lt; "Timed out.\n" &lt;&lt; endl;
  }
  return 0;
  }
  </code></pre>