+ 1
Please tell me how to make a delay of 10 seconds in c++ code. Thank you in advance.
EN Please tell me how to make a delay of 10 seconds in c++ code. Thank you in advance. DE Bitte sagen Sie mir, wie man eine Verzögerung von 10 Sekunden in C ++ Code macht. Vielen Dank im Voraus. RU Подскажите пожалуйста, как сделать задержку на 10 секунд в коде c ++. Заранее спасибо.
7 Réponses
+ 4
When programming on Sololearn, the result is always sent in one go after the program terminates, so you won't be able to add a delay like that. Putting a sleep anywhere will just delay the whole output.
Try this on your computer (if you need a C++ IDE, I suggest picking CodeBlocks).
#include <iostream>
using namespace std;
int main(void) {
cout << "Hallo, ";
_sleep(10000);
cout << "world"<< endl;
return 0;
}
If it doesn't work, try using sleep() instead and adding either
#include <windows.h>
or
#include <unistd.h>
depending on your OS (Windows or Unix/Linux-based respectively).
+ 2
Zen, thanks. The working version looks like this: https://code.sololearn.com/cGsa9f8rbi66/?ref=app . The "<unistd.h>" library and "sleep(!second!-no microsecond)" function are used.
In the sololearn, it naturally does not work.
This is the code I used:
#include <iostream>
#include <unistd.h>
using namespace std;
int main() {
cout << "Hallo,";
sleep(10) ;
cout << "world"<< endl ;
return 0;
}
+ 1
sleep(10)
0
Gordon, аn error is generated!
https://code.sololearn.com/cL21OrsgFPiT/?ref=app
0
Zen, Rewa Mathur, thanks.
0
Rewa Mathur, I used your code in a third-party program. He brought an error. Perhaps this is where it works, but not for me. What I posted is the only thing that pulls my program. Tomorrow I will try to run your code on the PC. I will definitely write about the result.