Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

+3 votes
asked May 14, 2020 by Tina Huang (150 points)

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

4 Answers

+2 votes
answered May 15, 2020 by LiOS (6,420 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, 2020 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, 2020 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, 2020 by Peter Minarik (87,810 points)
edited Jul 21, 2020 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().

commented Sep 12, 2023 by Éder S. (320 points)
here is more complete.


#include <iostream>
#include <unistd.h> // PUT IT CODE

using namespace std;

int motor ;

int main()
    cout<<"type ON TO START 3 MOTORS"<<  "\n";
    cin >> motor ;
    sleep(2)  ; // SLEEP 3 SECONDS.
    cout << " ON  motor 1 " <<  "\n";
    sleep(2)  ;
    cout << " ON motor 2 " <<  "\n";
    sleep(2)  ;
    cout << " ON motor 3 " <<  "\n";
    sleep(2)  ;
    cout << " ALL ON AND Ok " <<  "\n";
    sleep(2)  ;

    return 0;
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.