samedi 27 juillet 2019

Wait for a user input for 5 seconds and use a default value otherwise in c++

well, I'm working on a project that asks the user to input a number for 5 seconds and if the user fails to enter the number in 5 seconds the program aborts itself. I'm really stuck because when I ask the input from cin function it stays until the user enters the number.

I've tried sleep functions but it doesn't work

time_t futur = time(NULL) + 5;
while(true)//time(NULL) < futur)
{    
cout<<"\n-Press 0 to De-Activate Auto Abort... ";
chrono::duration<int, std::milli> timespan(5000);
this_thread::sleep_for(timespan);
if(time(NULL) > futur)
{   
cin>>Abort;
if(Abort==0)
{
break;
}
else
exit(0);
}

Aucun commentaire:

Enregistrer un commentaire