Como usar um timer em C ++ para forçar a entrada dentro de um determinado tempo?
Eu quero implementar um recurso de tempo limite em C ++.
Se o usuário não inserir o valor dentro de 2 segundos, o programa deverá exibir a instrução de tempo limite e solicitar a entrada novamente
EX (TELA DE SAÍDA):
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;
}
Eu trabalhei no código acima. Mas isso acontece apenas ao entrar no loop WHILE. Como devo fazer isso de maneira a obter a saída necessária.