1. Recommended sleep ();
Ms VC + + can use MFC's sleep function, the parameter is milliseconds.
Included in header file <windows.h>
/*#include<iostream>
#include<windows.h>
using namespace std;
void main()
{
Sleep(1000); //延时1秒
cout<<"adsd"<<endl;
Sleep(10000); // 注意S大写
cout<<"123"<<endl;
} */
2.delay ();
The delay function must be written by itself, not in the compiler.
#include <time.h>
void delay(int sec)
{
time_t start_time, cur_time; // 变量声明
time(&start_time);
do { time(&cur_time);
} while((cur_time - start_time) < sec );
}
And then you can call it directly.
Such as:
#include<iostream.h>
#include <time.h>
void delay(int sec)
{
time_t start_time, cur_time; // 变量声明
time(&start_time);
do {
time(&cur_time);
} while((cur_time - start_time) < sec );
}
void main()
{
cout<<"a"<<endl;
delay(5); // 滞后5秒
cout<<"b"<<endl;
}
Delay, shorter than one second, can be written like this:
clock_t start_time, cur_time;
start_time = clock();
while((clock() - start_time) < 3.0 * CLOCKS_PER_SEC)
{
}
But some compilers do not support clock