1.使用ctime
ctime里的CLOCKS_PER_SEC 等于该环境下每秒钟含有的系统时间单位数
结合以下代码可以得到预定时间:
float WaitTime;
cin>>WaitTime;
clock_t DelayTime=WiatTime*CLOCKS_PER_SEC;
而clock()将返回此时时间,则可以用以下代码获取当前时间
clock_t NowTime=clock();
完整代码如下:
#include<iostream> #include<ctime> #include<time.h> using namespace std; int main(){ cout<<"How long you want to wait for? \n"; float time_wait_want; cin>>time_wait_want; clock_t delay_time=CLOCKS_PER_SEC*time_wait_want; clock_t now_time=clock(); while(clock()-now_time<delay_time){ cout<<"wait\n"; } cout<<"Okay"; return 0; }