0

I am trying to write a simple program, but when i run the program, the console shows u, prints the output and then immediately disappears. Is there anyway to stop the console window from disappearing? or to make cout print to the output window in the bottom of Microsoft visual studio?

any help would be greatly appreciated :)

Graeham Broda
  • 131
  • 1
  • 6
  • 15
  • Put a breakpoint at the last statement of `main()`. – πάντα ῥεῖ Feb 07 '16 at 18:19
  • Since you're in Visual Studio, you can use `Start without debugging` (ctrl+F5) to run without attaching the debugger. This will run your program from a wrapper command prompt, and show 'Press any key to continue' after your program exits. If you want to write to the VS output window, consider using the Win32 `OutputDebugString` function (but not as a replacement for `cout`/`printf`). – Cameron Feb 07 '16 at 18:21

3 Answers3

1

One of the ways... In the beginning of file:

#include <conio.h>

And where you want to pause, write:

_getch();

It will wait until you press any button.

1

In Visual Studio, if you hit ctrl-F5 (start without debugging), it will pause after execution, showing Press any key to continue.

If you hit F5 (Start with debugging), the Console window closes immediately after execution.

Adi Levin
  • 5,165
  • 1
  • 17
  • 26
0

You can either compile in Debug and put a breakpoint at the last line of main() or add this below your functional code:

std::cin.get();

This method requires you to include <iostream>.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122