C++写一个限时输入功能,超过时间执行其他操作

写lab的时候需要实现一个限时输入功能,弄了好久终于弄好了,不多说,上代码:

char getDirection() {
char a;
time_t timeBegin = time(0);
int n=0;
    while(true)  {           //the main loop  ||  主循环
        if(kbhit()){       //detect the keyboard  ||  kbhit检测键盘输,如果发现了输入
            a = _getch() ;
            return a;
        } else if(time(0)-timeBegin==27 && n==0) {
            cout << "Your time have "<<3 <<" s"<< endl;//输出倒计时
            n=1;
        }
        else if(time(0)-timeBegin==28 && n==1) {
            cout <<  "Your time have "<<2<<" s"<< endl;//输出倒计时
            n=2;
        }
        else if(time(0)-timeBegin==29 && n==2) {
            cout << "Your time have "<<1 <<" s"<< endl;//输出倒计时
            n=3;
        }
        else if(time(0)-timeBegin>=30){//时间过去30秒,进行其他操作
            timeBegin = time(0);
            n=0;
//调用其他方法进行其他操作
        }
    }
}

猜你喜欢

转载自blog.csdn.net/dulingmo/article/details/81077071
今日推荐