使用<ctime>头文件,调用其中的常量CLOCK_PER_SEC,使用秒数 secs*CLOCK_PER_SEC可以保证,系统等待指定的时间而不会应为平台的不同而变化。
#include <iostream>
#include <ctime>
int main(){
cout<<"Enter the seconds: "; float secs; cin>>secs;
clock_t delay = secs * CLOCKS_PER_SEC;
cout<<"System defined clocks per second: "<<CLOCKS_PER_SEC<<endl; cout<<"System time: "<<clock()<<endl;
cout<<"starting\a\n";
clock_t start = clock(); //返回个clock_t对象,为一个long整数,表示开始程序到调用clock()时,程序经过的计时单元
while(clock() - start < delay){ //当前时间减去开始时间
;
}
cout<<"done\a\n";
cin.get();
cin.get();
return 0;
}