#include<winsock.h>
#include<iostream.h>
#include<conio.h>
// file descriptor for standard input
int main(void)
{
struct timeval tv;
fd_set readfds;
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
int STDIN =_fileno(stdin);
FD_SET(STDIN, &readfds);
// don’t care about writefds and exceptfds:
select(STDIN+1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(STDIN, &readfds))
cout<<"A key was pressed!\n";
else
cout<<"Timed out.\n";
getch();
return 0;
}
I intended the above program to wait and watch for 2.5 before timing out or print "a key was pressed !" if a user presses a key. But almost immediately it prints "A key was pressed", even if was not. Any suggestions?