What ways to pause the program for a time (sec) do you know?
I suggest this:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
double tim;
tim = time(0);
while(time(0) - tim != 2) { }
cout << "hello world"!;
while (time(0) - tim != 2) { }
}
Any other options?
0
5 answers
#include <unistd.h>
...
...
sleep(2);//2 секунды
usleep(1000000);//1 секунда (1.000.000 микросекунд)
Note that when checking these functions, printf must either end the print with "\n"
, or set flush: fflush(stdout);
immediately after the print, for example:
printf("1");
fflush(stdout);
sleep(1);
printf(" - 2");
The same applies to cout. The analog of fflush(stdout) is cout.flush();
PS There is no such thing in Windows, but there is a replacement:
#include <windows.h>
Sleep(1000);//1 секунда - 1.000 миллисекунд
6
Author: ivkremer, 2011-08-01 14:05:55
The canonical C++ method is to use std::this_thread::sleep_for
#include <chrono>
#include <thread>
int main() {
std::this_thread::sleep_for(std::chrono::seconds(2));
}
Starting with C++14, you can write
using namespace std::literals;
std::this_thread::sleep_for(2s);
3
Author: Abyx, 2015-08-30 11:19:25
- sleep()and delay () - from the standard c library (I don't remember which one),
- Sleep () - WinAPI from windows. h
- Use for, while loops for the delay. I also heard that there is a wait () function;
0
Author: Alexey Emelyanenko, 2011-08-01 18:26:36
Sleep () and delay() here's a good article
0
Author: Александр Кавокин, 2011-08-14 12:35:19
#include <iostream>
int main(){
using namespace std;
for (float ex = 0; ex <=4; ex=ex+0.10) {
system("cls");
cout<<"Exit through 4 second"<<endl;
cout<<" ";
}
exit(0);
return 0;
}
As an option)
-1
Author: Влад Красношапка, 2017-01-08 09:44:51