+ 1
output
hello. does anybody here know how to make an output only appears for 3-5 seconds in the runtime using c++? im trying to do my final project here.
1 Answer
+ 2
To make your output in the end or at any time appear for a finite time before continuing to any other operation, you can halt the current execution flow for a particular duration.
If you have access to a compiler with C++11 or greater support, you can use sleep_for to stop the current running thread (or your program's execution, which is a single thread here) for a finite amount of time.
Eg :
this_thread::sleep_for(2s);
// Makes the current thread wait
// for 2 seconds.
// You can also wait in milliseconds,
// nanoseconds, minutes, and hours.
Here is more information on the function:
https://en.cppreference.com/w/cpp/thread/sleep_for
You can also use WinAPI's Sleep() or POSIX's sleep() function, if you don't have a C++11 compiler. These methods are, however, OS dependent.