Alternative to system("pause")

I want to have a waiting or something like system("pause"), but it has been said to be evil. Is there any alternative? cin.get or cin.ignore do the purpose but I want it to be able to do any key combination, not just by pressing enter.

1. anything exactly like system("pause") but not using system();
2. also is there anything like system("CLS") but not using system();
There is no alternative.

Just don’t do it.



To explain: the system("pause") is evil for a myriad of reasons, but in this case, it is specifically evil for distracting you from the true problem: incorrect program invocation.

Console programs are meant to be run from the console.

Yes, yes, I know that your IDE allows you to press Ctrl+F9 (or whatever) and run it directly. Some IDEs are even smart enough to keep the shell window open for you after your program has terminated. While playing with the IDE, go ahead and stick that PAUSE in there.

Just don’t forget to REMOVE IT before you give your program to anyone, like your professor.


I personally just open a Windows Console (tricked out with all my own configurations, but that’s a story for another day) and change to the directory that the IDE/Compiler put your program’s executable in. You can even get there from Explorer by navigating to the .EXE and clicking on the address bar and typing “cmd” and pressing Enter.

Thereafter, have your IDE compile your program without executing it, then switch to the Console Window and type your program’s name. And if you want to run it again (say, because you recompiled, or want to try with different inputs) then you only need to press the Up Arrow key and Enter. Easy peasy.

Hope this helps.
So on the programming side just use cin.get or cin.ignore. Thanks, and for system("CLS") just print a bunch of lines to make it seem like it was cleared?
Keeping the console window open seems to be something that only Windows users are concerned with. I guess it's because on Windows when you double click on the executable file it will automatically open a console window that is automatically closed when the program terminates.

What happens on other systems when double clicking on an executable file is usually that it will run the program but it will not automatically open anything to show output or accept input. Users on these systems are more used to console programs and knows how to run them from the console. For a program that is meant to be run from the console it would just be irritating if it didn't terminate when it was finished but instead asked for more (useless) input.

The point that I'm trying to make is that if you insist that the user should have to press a key before the program terminates you probably only want this on Windows so it might be acceptable to use a solution that is Windows-specific.

If you decide to use cin.ignore() (or cin.get()) note that it will only wait for more input if the input buffer is empty and none of the error flags are set. To clear the error flags you can use cin.clear(). To make sure the input buffer is empty you can use cin.sync() (this seems to only work on Windows).

1
2
3
4
5
6
#ifdef _WIN32
cout << "Press enter to exit.";
cin.clear();
cin.sync();
cin.ignore();
#endif 

For some programs it can make sense to force the user to take an active step in order to close the program. In a game for instance you could ask the user if he want's to play again. If the answer is yes you just repeat, otherwise you just let the program terminate immediately. This makes the whole issue go away and it works fine everywhere but it's not suitable for all types of programs.

If you want more advanced control over the console window you might want to use the WinAPI or a library such as pdcurses. This would allow you to do things such as reading key presses directly without having to press enter. It would also provide you with simple functions for clearing the whole screen and for printing things at any position you like. You can even do double buffering to avoid flicker when the screen is updated.
Last edited on
If you want to add pause to Windows applications only:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
  std::cout << "Hello world!\n";
  
  void PAUSE(); PAUSE();
}


#ifdef _WIN32
#include <windows.h>
void PAUSE()
{
  std::cout << "Press any key to quit..." << std::flush;
  HANDLE h = GetStdHandle( STD_INPUT_HANDLE );
  FlushConsoleInputBuffer( h );
  WaitForSingleObject( h, INFINITE );
  FlushConsoleInputBuffer( h );
}
#else
void PAUSE() { }
#endif 

Enjoy!

[edit]
The clear,sync,ignore stuff is not guaranteed to do anything useful (and usually doesn’t).
Last edited on
Isn't it a bit confusing to have a function named PAUSE() if it doesn't always pause? I think it should either be renamed to something like pause_on_windows(), or only defined and called when compiling the program on windows.
The last time this was brought up, the person made a solution to put the pause functionality inside the destructor of an object made in main, pretty creative... but so futile, it's like giving a bandage to someone suffering from a heat stroke. I'm not really adding anything more than that Duthomhas has said, but I wanted to reiterate that it isn't just some IDEs, but pretty much every modern IDE that has a way of either stopping the console, or they have their own space for input/output (like Eclipse).

And if you're not working through an IDE, having a pause is a disadvantage -- so it's best to keep that pause separate from the program logic itself. For example, you could write a simple batch file that just looks like:
1
2
my_program
pause
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;


void PAUSE()
{
   string dummy;
   cout << "For goodness' sake press ENTER ... ";
   getline( cin, dummy );
}

int main()
{
   cout << "Calling PAUSE twice ...\n";

   PAUSE();
   PAUSE();

   cout << "Phew!";
}
all the alternatives require pressing multiple keys because of the blasted buffered IO or the solutions become insanely complex to resolve that issue. All to replace 1 line of code that people want to avoid on principle over practical.

Or to put it another way, until an unbuffered getch() is standard, there is a gap that pause fills.

@jonnin
Your conflict of thinking prejudices yourself into claiming incorrect things and reveals your indifference to the actual issue. One, that has, not-so-incidentally, bothered people for the last 20+ years. At least.

If I were to guess what the pause function of the cmd.exe looked like, I bet it would look something similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HANDLE hConsoleInput;
HANDLE hConsoleOutput;
...
void PauseCommand()
{
  const char* prompt = "    Press any key to continue . . .";

  // Print the user prompt
  DWORD dwNumberOfCharsWritten;
  WriteFileA( hConsoleOutput, prompt, lstrlenA( prompt ), &dwNumberOfCharsWritten, NULL );
  
  // Wait for the user to press any key and consume it
  FlushConsoleInputBuffer( hConsoleInput );
  WaitForSingleObject( hConsoleInput, INFINITE );
  FlushConsoleInputBuffer( hConsoleInput );
}

Hmm, looks awfully familiar...

And dang if it isn’t short and easy to do!
(One liners are for script kiddies.)
Topic archived. No new replies allowed.