For a program I'm writing, I wish to read in characters from the keyboard using cin until a certain amount of time has passed. The user can enter as many character as they wish (from 0 to however many possible), and the program needs to store the characters in an array for use later. Ideally, the user should not have to press enter after each letter; the program should be able to read in the input without any line breaks and stop once the specified time is up. I've been looking for a while and have tried different methods, including using getline, usleep, and even SDL, but nothing I've tried has worked. This is my latest attempt:
#include <iostream>
using namespace std;
#include <time.h>
const float frame_length = 1;
int main () {
char test[40];
clock_t t;
t = clock();
int counter = 0;
while (clock() - t < (1000000 * frame_length)){
cin >> test[counter];
counter ++ ;
}
cout << endl << "STOP" << endl;
}
The problem here is that the loop is not stopping after the specified time at all (when I replaced the cin line with a cout line it did) and the user still needs to press enter every time. Is it at all possible to read in input the way I wish? If it helps, I'm working on a Mac. Any help would be much appreciated.