C++ How do to have the program sleep for a few seconds???

+2 votes
asked May 14 by Tina Huang (140 points)

I tried doing Sleep(3000) but it doesn't work and #include<windows.h> isn't allowed

4 Answers

0 votes
answered May 15 by LiOS (5,120 points)
OnlineGDB is hosted on Linux not windows so need to use #include <unistd.h>, which is the Linux header file equivalent to windows.h
0 votes
answered May 16 by Dontwait00 (180 points)

You could use while and/or for loop. You could even use <time.h> to propperly check time per clock or seconds passed. An example, is to create a function containing this instructions.

0 votes
answered Jul 17 by Pragnesh Patil (140 points)

it is not that hard all you have to do is to use the <windows.h> as your header file and wherever you want it to sleep you have to use the function Sleep(time in miliseconds);

An example:-

#include <windows.h>

using namespace std;

int main()
    cout<<"hello this text will change after a pause of 10 seconds"<<endl;
    cout<<"this is the new text"<<endl;

   return 0;

welcome in advance :)

0 votes
answered Jul 20 by Peter Minarik (9,500 points)
edited Jul 21 by Peter Minarik

Though C code works in C++, I'd suggest checking out the C++ library and have a look at std::this_thread::sleep_for().

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.