Как использовать таймер в C ++ для принудительного ввода в течение заданного времени?
Я хочу реализовать функцию тайм-аута в C ++.
Если пользователь не вводит значение в течение 2 секунд, тогда программа должна отобразить оператор тайм-аута и снова запросить ввод
EX (ВЫХОДНОЙ ЭКРАН):
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`
Код:
#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;
}
Я работал над кодом выше. Но это происходит только при входе в цикл WHILE. Как мне сделать это так, чтобы я получил требуемый результат.