How to pause a Console application in C++ on Unix/Linux or the equivelent to the windows C++ system(”pause”)statement or the _getch() functions in conio.h?

So in C++ on windows, there are two ways to get this feature:

Calling this line, which really is making a call to system to open a cmd.exe process and run “pause” inside the cmd.exe process. So this is really not something that should be in production code.

system("pause");

Or using _getch() from conio.h, which is probably something that would be better for “windows only” production code. At least it is better than doing a system call to open a separate cmd.exe process to run “pause” and it takes less resources.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	std::cout << "Press any key to continue...";
	_getch();
}
&#91;/sourcecode&#93;

These above options give you two main features in Windows:

1. Your console application pauses.
2. You can get the option to "Press any key to continue . . ." which allows you to press ANY key on the keyboard to continue.

<blockquote>What is the equivalent in Unix\Linux?</blockquote>
First, there is no equivalent to do exactly what you want on Unix/Linux platforms.  You really can only get these two features on Unix/Linux in C++.

1. Your console application pauses.
2. You can get the option to "Press [ Enter ] to continue . . ." which mean that presses anykey on the keyboard isn't going to work, only pressing Enter will work.

<strong>Why can you not get your C++ program to accept ANY KEY in Unix/Linux?</strong>
Now, in my research it appears that your C++ program does not take control of your keyboard and read every key stroke.  Instead, it waits for the console or tty to actually pass the keyboard strokes to the program, which usually happens when you press [Enter] and not before. This is called "line-buffered" input.  While Microsoft's CMD.EXE (which is a type of console or tty on windows) passes each key stroke to your program immediately and is not "line-buffered", which is why this works on windows.

So these differences are not something you have control over, unless you want to write your own Linux/Unix console or write your program to somehow interface with the console or tty to disable "line-buffered" input and get each key stroke passed to your program.  In my research I found some say you could configure this but I couldn't find any concrete examples.

<strong>So how do I at least get the "You can press [Enter] only to continue." option?</strong>

Here is how I did it, feel free to give me feed back if I left something out.

<strong>Doing it all in main.cpp</strong>


#include <iostream>

using namespace std;

int main()
{
	doPause();
}

void doPause()  // I had an error on FreeBSD because there was already a pause() function elsewhere, so I changed it to doPause();
{
	std::cout << "Press &#91; Enter &#93; to continue...";
	cin.clear(); // Make sure the buffers are clear, you may want to do cin.flush() as well but I am not sure if both are needed.
	cin.ignore();
}
&#91;/sourcecode&#93;

<strong>Doing it as a class or object</strong>

main.cpp

#include <iostream>
#include <Pause.h>

using namespace std;

int main()
{
	new Pause();
}

Pause.h

#include <iostream>

class Pause
{
	public:
		Pause();
};

Pause.cpp

#include
#include “Pause.h”

Pause::Pause()
{
std::cout << "Press [ Enter ] to continue..."; cin.clear(); // Make sure the buffers are clear, you may want to do cin.flush() as well but I am not sure if both are needed. cin.ignore(); } [/sourcecode] When I get an example of how to do by turning of "line-buffered" input, I will update this blog.

One Comment

  1. Sean says:

    I tried using your code:
    cout << "blah...;
    cin.clear();
    cin.ignore();

    but it failed to properly pause the unix terminal screen in my c++ program. That said, adding cin.get(); as the last line of code in the pause() function made it work perfectly. Thanks for the starter though.

Leave a Reply

How to post code in comments?