Making C++ pause - c++

Is there a C++ equivalent to Python's time.sleep()?

Use boost::this_thread::sleep
// sleep for 5 seconds
boost::this_thread::sleep(boost::posix_time::seconds(5));

The following code will sleep for 10 milliseconds.
boost::this_thread::sleep(boost::posix_time::milliseconds(10))
Refer to boost::posix_time::time_duration for more ways to construct the duration.

I'm not aware of any portable function, but mainstream OSes have usleep for *nix and Sleep for Windows.

Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch

Related

Command system("pause") not found on Linux

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main() {
string line;
cout << "HW\n";
getline(cin,line);
cout << "Your line is - " << line << "\n";
system("pause");
return 0;
}
I want to do gui to factorio headless server by myself so i need to exec few bash scripts. I think i need function system() to that ?
I think I got problem with lib path. Please don't blame to wrong installed vcpkg. Paths is :
/opt/factorio/bin/x64/vcpkg/installed
/usr/include/c++/9/x86_64-redhat-linux
/usr/include/linux
/usr/include/c++/9/tr1
Command system() not found says Visual Studio.
system("pause"); is meant to be used only on Windows. It runs the Windows command-line "pause" program and waits for that to terminate before it continues execution of your program. That's why it's a bad practice to use it in your code, no matter if you are running your code on Windows or Linux.
Here is a better way you can achieve the same result:
#include <iostream>
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
instead of:
#include <iostream>
using namespace std;
int main() {
system("pause");
return 0;
}

Pause For-Loop for 1 second in C++

I have an loop, but it goes to fast. I need something simple and easy to use, to pause it for 1 sec in each loop.
for(int i=0;i<=500;i++){
cout << "Hello number : " << i;
//i need here something like a pause for 1 sec
}
std::this_thread::sleep_for is exactly what you're looking for.
for(int i=0;i<=500;i++){
cout << "Hello number : " << i;
std::this_thread::sleep_for(1s);
}
To use it like that, you need to include <chrono> and <thread> and then add using namespace std::chrono_literals;. It also requires c++11 enabled.
Sleep(n) is a prepared method. To use this method do not forget to add "windows.h" header file as well and remember 'n' is the millisecond you may wish to delay the code execution. A simple code which repeats "Hello world!" can be seen:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
{
cout << "Hello world!" << endl;
Sleep(1000);
}
return 0;
}
If you use windows platform this may help:
#include <windows.h> //winapi header
Sleep(1000);//function to make app to pause for a second and continue after that

ncurses getch() weird output?

I have installed the ncurses.h lib and started experimenting with the getch() function.When I built and run this code which seems alright to me at first, the console printed out a weird character: '�'(if it doesn't show and shows as a space here is a screen shot: https://prnt.sc/gbrp7b) The console starts spamming it but if I type a character, it shows up in the output but still the '�' spams. Here is the code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
cout << "You entered : " << input << endl;
//break;
}
return 0;
}
So I thought of trying to use an if statement to try and stop it spamming but the code doesn't recognise the character:
It gives this error:
error: character too large for enclosing character literal type
For this code:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
while(true){
input = getch();
if(input!='�'){
cout << "YOu entered : " << input << endl;
}
}
return 0;
}
I am on OSX Sierra 10.12.5 and using eclipse Oxygen
You need to initialize ncurses with initscr() and close it with endwin() functions:
#include <iostream>
#include <fstream>
#include <ncurses.h>
using namespace std;
int main(){
char input;
initscr();
while (true) {
input = getch();
cout << "YOu entered : " << input << endl;
}
endwin();
return 0;
}

Dev-C++ Hello world doesn't show

I am new to C++. I downloaded and run Dev-C++ and I write and run F9 this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
return 0;
}
But no "Hello, world!" is printed, why?
Many IDE users have this problem. The program runs but it closes before you can see its results on the screen. One portable fix is to add this at the bottom of main before you return:
std::cin.get();
That way it will wait for you to enter some text before it exits.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
getchar();
return 0;
}
Add getchar() at the end of your program as a simple "pause-method" as consoles seems to close so fast, so you need to "delay" to see your console.
The output is printed to a terminal, and you don't have a newline etc.... very unlikely that you will see it, so
Add a newline to the output
make sure you have time to read the output before the terminal window closes (add a sleep or something)
Don't use using namespace as that is a bad practice and will lead to trouble in your programming.
So like;
#include <iostream>
#include <unistd.h>
int main()
{
std::cout << "Hello, world!" << std::endl;
sleep(2);
return 0;
}

How do you make a program sleep in C++ on Win 32?

How does one "pause" a program in C++ on Win 32, and what libraries must be included?
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
If you are using boost, you can use the thread::sleep function:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
sleep(unsigned int)
The value has to be a positive integer in millisecond.
That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
system("PAUSE")
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add
the following line at the biggining of the code :
#include <cstdlib>
cin.ignore()
The same result can be reached by using cin.ignore() :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
cin.get()
example :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
getch()
Just don't forget to add the library conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
If you wish for the program to stay responsive while "paused", you need to use a timer event.
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.