I need to make program wait for some time until continue. I tried: system("stop")
but console output was:
" 'stop' is not recognized as an internal or external command, operable
program or batch file.". I need answer in c++. Answers in Is there a decent wait function in C++? doesn't work.
Asked
Active
Viewed 186 times
-4
-
2Waiting for what? – John Coleman Feb 08 '18 at 19:46
-
Waiting some time. – jaksia Feb 08 '18 at 19:47
-
3There are many ways to cause a thread to block. It depends on why you are blocking. If you just want to wait for a fixed amount of time, look at http://en.cppreference.com/w/cpp/thread/sleep_for – François Andrieux Feb 08 '18 at 19:47
-
So you want the program to *pause*? – NathanOliver Feb 08 '18 at 19:48
-
Yes. I want to pause program. – jaksia Feb 08 '18 at 19:49
-
1Sounds like you want your program to *sleep* rather than *pause* – John Coleman Feb 08 '18 at 19:49
-
_Probably yes._ – jaksia Feb 08 '18 at 19:53
-
6Possible duplicate of [Is there a decent wait function in C++?](https://stackoverflow.com/questions/902261/is-there-a-decent-wait-function-in-c) – John Coleman Feb 08 '18 at 19:57
-
1It sounds like you're not sure what you want. Can you provide more background on what you're trying to accomplish with this? It is solely to look at the console output after execution? – Justin Randall Feb 08 '18 at 19:57
-
1@jaksia As a portable c++ solution you should have a look at [`std::this_thread::sleep_for()`](http://en.cppreference.com/w/cpp/thread/sleep_for) to really pause CPU consumption of your code for a minimum guaranteed amount of time. – Feb 08 '18 at 20:13
1 Answers
4
Following makes your program stop. At least for the next year or so. Roughly. Bonus: Its portable and doesn't drain your laptop's battery.
#include <chrono>
#include <thread>
int main() {
using namespace std::chrono_literals;
std::this_thread::sleep_for(8760h);
return 0;
}

Ben Kircher
- 152
- 1
- 7
-
1Also, you could pass `seconds`, `milliseconds` etc. You can look up for values that can be passed in `std::this_thread::sleep_for` – Aman Feb 09 '18 at 08:28
-