Pausing a Console Program

Click here to change the theme.

I often see questions asking how to pause a console program being debugged. Visual Studio (and its predecessor) will pause a console program being executed from within it without the debugger. That is different from the behavior that we see when the program is executed somewhere else, such as from Windows Explorer and from "Run" in the Start Menu. Visual Studio does automatically pause a console program when the program is being debugged. So we usually need to do something separate to pause a console program being debugged so we can see the results.

What I usually do is to put a breakpoint on the last line of main, or somewhere like that.

Often the suggestion is to use system("pause") or cin.get(). Since that will always cause the program to pause even if the program is not being debugged, it can be slightly irritating. We certainly do not want to leave that in a production program. A good solution is to use the IsDebuggerPresent function in combination with cin.get() to determine if
the console program is being debugged. The following is a simple sample of how it can be used.

if (IsDebuggerPresent()) {
	std::cout << "Press Enter to continue ...\n";