Delay function?

Hi all, I have just started to work with c++, and I was wondering if I could have a delay function. For instance:

1
2
3
4
5
6
7
8
 #include <iostream>
int main()
{
std::cout<<"hello"<<std::endl;
--delay
std::cout<<"hello2<<std::endl;
return 0
} 


What would I write out for "delay?"

Thanks all :)
Several options are available. See the last post here for C++.
http://www.cplusplus.com/forum/beginner/139609/
found no essy example there. i got this essy one, though its not much preferable cause it uses cpu instead "sitting idle"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//halt! uses cpu
#include <iostream>
#include <ctime>

int main()
{
    std::time_t start = time(0);
    
 	std::cout << "hello ";
 	
    while( difftime(std::time(0), start) <=3 ) ;    
    
 	std::cout << "world";
	  
}

Edit: it worked in my Dev C++ and in Visual C++ 2012.
but not directly in online compilers - i dont know how about the exe file created by them
Last edited on
closed account (1CfG1hU5)

your language may have a delay function.

delay(2000); // 1000 milliseconds per second.

in older languages is defined in dos.h

anup30 wrote:
found no [easy] example there

Only because you didn't bother to read the thread I linked. Otherwise you would have seen the short, 19-line example that'll work anywhere.

Or, if you had even read the entirety of my 11-word post here, you would have known to look at the last post in that thread to find

this_thread.sleep_for( N ).

But you probably won't bother to click the search box at the top of this page and type in something like "sleep_for", which'll get you this:
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/?kw=sleep_for
and lead to information like this:
http://www.cplusplus.com/reference/chrono/duration/?kw=duration
and this:
http://www.cplusplus.com/reference/chrono/milliseconds/

I imagine that

1
2
#include <chrono>
#include <thread> 
 
std::this_thread.sleep_for( std::chrono::milliseconds( 150 ) );

isn't easy enough?


It just really hacks me off when you hand people an answer and they're too glossy-eyed to read it. Oh, but my CPU-chugging version is so much better!
^this post shows the exact example i wanted. yesterday i went to your link ...
actually tried your this_thread.sleep_for( N ) (used 3 in N) but couldn't make it work. didn't search further branches as a beginner (hope you don't mind). searched some ctime reference in this site, couldn't make sleep() work. i didn't go to cppreference.com because that seems too technical for a beginner.
at last i thank you for providing reference to one line solution - which i was looking for. (my previous code isn't a solution actually, rather its a question in disguise - so it caught the required attention. now you may remove your subscripts, i have not read them - believe it or not :)
I didn't go to cppreference.com because that seems too technical for a beginner.
Reference for programmers is what dictionary is for someone learning foreign language. It might looks intimidating ("What are those n, v or other leters near words?") but if you skip irrelevant information (like possible implementation) you will find it extremily useful. It provides complete answer to question "What that function do?"

Bonus: In C++14 following is possible — std::this_thread.sleep_for( 150ms );



@anup30
Sorry I was so grouchy.
Registered users can post here. Sign in or register to post.