我希望在C++中实现一个超时功能。
如果用户在2秒内没有输入值,则程序必须显示超时语句并再次要求输入。
例如(输出屏幕):
代码:
我已经在上述代码上工作了。但是这只发生在进入 WHILE 循环时。我应该怎样做才能获得所需的输出。
如果用户在2秒内没有输入值,则程序必须显示超时语句并再次要求输入。
例如(输出屏幕):
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 循环时。我应该怎样做才能获得所需的输出。