-2

I was recently messing around with c++ console programming. I was wondering if there was a way to make text appear on the console for a specific amount of time, then go to some more text. Essentially, I'm trying to create a timer object. Or if you're familiar with python, it would be something like

import timer

print "Hello World"
timer.sleep(2)
print "Hello Again World"
timer.sleep(2)

If someone could help me with this, I would appreciate it, thanks in advance.

emufossum13
  • 377
  • 1
  • 10
  • 21
  • 2
    possible duplicate of [sleep for milliseconds](http://stackoverflow.com/questions/4184468/sleep-for-milliseconds) or http://stackoverflow.com/questions/3379139/sleep-function-in-windows-using-c or http://stackoverflow.com/questions/2252372/how-do-you-make-a-program-sleep-in-c-on-win-32/ – user7116 Aug 23 '13 at 18:13
  • for some reason, when I use the sleep function, sleep(x milliseconds) it says that, the function is not recognized. Is there a specific stream i have to include first to use the sleep function? – emufossum13 Aug 23 '13 at 18:14
  • 1
    http://stackoverflow.com/questions/1658386/sleep-function-in-c Somebody covered this topic already. – khajvah Aug 23 '13 at 18:14
  • 1
    http://en.cppreference.com/w/cpp/thread/sleep_for – Zac Howland Aug 23 '13 at 18:17
  • I can't find a `sleep` function in any Python module named *timer*; which module are you talking about? And is this question really about displaying text for a certain amount of time? Does the Python module actually *remove* anything printed to the console after time elapses? Or are you really asking how to *delay execution* of your program for some time, and the text displayed is completely irrelevant? – Rob Kennedy Aug 23 '13 at 18:19
  • I'm sorry, I meant "time", not "timer". However, they may have taken this class out in the new Python 3.x. I was referring to Python 2.6, which is what I used to create a text based game that relied heavily on the time class. – emufossum13 Aug 23 '13 at 19:05

2 Answers2

3

Before C++11 there was no standard way to do it, either use a system library or use a cross platform library that wraps system libraries for you. In C++ 11 it takes the thread, and chrono libraries to get it done.

#include <iostream>
#include <chrono>
#include <thread>
int main()
{
    std::cout << "Hello world" << std::endl;
    std::chrono::milliseconds twoSeconds( 2000 );
    std::this_thread::sleep_for( twoSeconds);
    std::cout << "Hello Again World" << std::endl;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
stonemetal
  • 6,111
  • 23
  • 25
  • ...where "a cross platform library" stands for [boost](http://www.boost.org/doc/libs/release/doc/html/thread/thread_management.html#thread.thread_management.this_thread.sleep_for) – Cubbi Aug 23 '13 at 21:25
1

There's no built-in C++ functionality for this, you have to resort to platform-specific functions.

On POSIX systems (Linux, Mac OS X, BSD, ...) you can use sleep from <unistd.h> (for delay with second resolution) or nanosleep from <time.h> (nominal resolution of nanoseconds, but much worse in practice). On Windows, instead, you can use the Sleep API from <windows.h> (nominal resolution of milliseconds).

---edit---

As specified in the comments, in C++11 there's std::this_thread::sleep_for.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 5
    There is in C++11: `std::this_thread::sleep_for`. – chris Aug 23 '13 at 18:16
  • Thank you very much, I wound up using the Sleep API for windows like you said. I also was able to craft a function that passes a variable s, and multiply's that by one thousand (the equivalent number of milliseconds to seconds), then runs the sleep function based on that variable. This way, one could easily define how many seconds they wanted the function to run for, instead of figuring it out in milliseconds. – emufossum13 Aug 23 '13 at 18:31