'Delay in Dev C++
I have come across to a problem while coding in C++ on Dev C++ compiler. I want to delay my statement to some milliseconds, but the problem is dev doesnt support the dos.h header file and so its contents as well. I had an alternative way for using it with the help of for loop but i aint got its proper syntax in my mind to use it properly. I wish, you folks, might be able to resolve the problem for me. Thanks in Advance..
#include <dos.h>
int main(){
delay(1000)
cout << "Hello"
return 0;
Tell me another alternative way for this please.
Solution 1:[1]
C++ has < thread >
< thread > has "std::this_thread::sleep_for(...)"
Example illustrating the (...)
std::this_thread::sleep_for(100ms);
The ms of 100 ms comes from
using namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30 us
Probably you also need to include < chrono >
Solution 2:[2]
Use the header chrono and thread
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::this_thread;
using namespace std::chrono;
sleep_for(nanoseconds(10));
sleep_until(system_clock::now() + seconds(1));
You can change the nanoseconds part to seconds or keep it at nanoseconds, and the number beside it is the amount of that unit that you can change to any number.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Keenan Ravi |