Using the system(“pause”) command in C++

Last Updated On Thursday 18th Nov 2021

system pause c++

system("pause")

  • This is problematic & annoying.when you run the program from the command line.
  • To keep the program’s console window open at the end when you start it from Visual Studio, use Ctrl+ F5 to start it without debugging.

  • For interactive execution, you must press a key at the end without any target.

  • This method of suspending is highly ineffective by normal C ++ programming standards, it doesn’t make any difference at the end of a beginner’s program.

  • So, the statements in the horde of answers before are wrong, and the main reason why you should not use system("pause")

It’s not portable. This works only on systems that have the PAUSE command at the system level, like DOS or Windows. But not Linux and most others.

You can use std::cin.get() from iostream.

	#include <iostream> // std::cout, std::cin
using namespace std;

int main() {
   do {
     cout << '\n' << "Press the Enter key to continue.";
   } while (cin.get() != '\n');

   return 0;
}
	

It’s a very expensive and resource heavy function call. It’s like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is

  • suspend your program
  • call the operating system
  • open an operating system shell (relaunches the O/S in a sub-process)
  • the O/S must now find the PAUSE command
  • allocate the memory to execute the command
  • execute the command and wait for a keystroke
  • deallocate the memory
  • exit the OS
  • resume your program

In addition, system('pause') working slowly and include the file that you probably do not need: stdlib.h.

It is platform dependent and actually calls the 'virtual' OS.