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

0 votes
answered May 15, 2020 by LiOS (6,240 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 (160 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>
#include<iostream>

using namespace std;

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

   return 0;
}

welcome in advance :)

0 votes
answered Jul 20, 2020 by Peter Minarik (22,620 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().

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.
...