¿Cómo usar un temporizador en C ++ para forzar la entrada dentro de un tiempo dado?
Quiero implementar una función de tiempo de espera en C ++.
Si el usuario no ingresa el valor dentro de 2 segundos, entonces el programa debe mostrar la declaración de tiempo de espera y volver a solicitar la entrada
EX (PANTALLA DE SALIDA):
Timer=0;
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input:22
Data accepted
Terminate the program`
Código:
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t endwait;
endwait = 2000 ;
cout<<endwait;
while (clock() < endwait)
{
cout<<"Please enter the input:";
}
return 0;
}
He trabajado en el código anterior. Pero esto sucede solo al ingresar al ciclo WHILE. ¿Cómo debo hacerlo de una manera para obtener el resultado requerido?