16

After compiling console programs the console window closes immediately after running. What is the best practice for keeping it open? I've searched google loads, I'm used to codeblocks where you don't have to worry about it, but, I want to mess around with Visual Studio a bit and with VS, my console closes. All over the interwebz there are several different ways to keep it open, however, I've read that most of them are bad coding techniques. What is everyones preferred method?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Lonestar
  • 269
  • 1
  • 4
  • 11

11 Answers11

22

When I'm using Visual Studio and I don't need debugging I just run it using Ctrl+F5 keystroke – and it prevents console from closing.

Community
  • 1
  • 1
Regent
  • 5,502
  • 3
  • 33
  • 59
13

Since you always run in the debugger, set a breakpoint on the return statement from main().

The debugger is your best friend, learn (and learn early) to use it to your advantage at every opportunity.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
6

Run your program in a console, which would be the expected way of running a console program ...

Alternatively, you can make a little batch file to execute your program that would have:

REM batch file to launch program
yourprogram.exe
PAUSE

and the PAUSE cmd.exe command will ask to user to press any key.

erjiang
  • 44,417
  • 10
  • 64
  • 100
  • 9
    It's fairly easy actually. Right click on your project in VS, choose Properties. Under the debug section you can tell it to start an external program. In this case, it would be the batch file that launches your application. – Timothy Strimple Jul 23 '09 at 17:39
6

I tend to use system("PAUSE"); which gives you a

Press any key to continue . . .

message.

irh
  • 668
  • 5
  • 11
  • 1
    This is windows specific I think, but it does get the job done. – DeusAduro Jul 23 '09 at 17:21
  • You're right it is Windows only, but I've only ever found this to be an issue in Visual Studio so figured it'd be OK.. – irh Jul 23 '09 at 17:28
  • 6
    Bit harsh.. Why not? It's a temporary one-liner to make Visual Studio behave in a more convenient way while developing an app, and not intended for production code. – irh Jul 24 '09 at 10:49
  • 2
    I have just learned this today: (http://www.gidnetwork.com/b-61.html) It explains why to not use system ("pause"); :) – Martin Berger Oct 01 '11 at 17:04
  • 1
    I'd agree that it's a bad idea to use it in production code, but the issues described in the link aren't a problem when you're just trying something out like the OP. – irh Oct 02 '11 at 15:51
5

cin is grossly inelegant but easy for the forgetful to derive:

{
  char c;
  std::cin >> c;
}

That holds the window open until you type a character /* edit */ and hit enter.

std::cin.get() will close the window the moment you type a character, which, depending on how easily you become habituated, runs a marginally greater risk of "whoops, I wish I hadn't closed that!" than the two-keystroke operator>>(istream &).

Both differ from a system("pause") in that they return in a program-accessible way the value of the character you typed, so, if as not infrequently happens, one kludge leads to another, you can write a switch statement based on what you typed to (for example) exit immediately, write some values to a log, run it again, etc.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
  • 3
    std::cin.get() has the same effect and is shorter – robson3.14 Jul 23 '09 at 17:59
  • 3
    It holds the window until it can get a character. Which in not until the buffer is flushed (cin is buffered). So it requires the user to hit enter (or type a lot of characters and fill the buffer). – Martin York Jul 23 '09 at 18:54
3

I use:

cin.get()

I heard it was less costly than system("PAUSE") and it works on POSIX systems too. There's a great link that goes into detail about this.

Steve
  • 11,831
  • 14
  • 51
  • 63
  • 4
    What does costly have to do with anything? The whole point of the code is to keep the terminal window hanging around until you press a key. How slow do you think `system("PAUSE")` is, compared with e.g. your reflexes ;-) – Steve Jessop Jul 23 '09 at 17:49
3

Resist the temptation to do anything. Well behaved command line programs exit when they've finished running reporting a status via their exit code. This enables them to be scriptable and 'good citizens' in automated environments. Even in an interactive environment, why force the user to make an extra key press just because of your debugging environment?

If you run, rather than debug then Visual Studio will open a console windows that pauses after your application exits so that you can still view the output. I don't know why the behaviour is different when you debug, perhaps because you have breakpoints available so if you want to see the output at various stages you can place breakpoints after the relevant output statements, or at the end of main or enable various 'stop on exception throw' options.

Whatever the reason, I've never felt compelled to compromise the behaviour of my application just to enhance my debugging experience.

Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2

A very common one is to just put in code to read a key from the console after your main application code closes. The keystroke read in just gets thrown away, but it holds the console open.

It's not pretty, necessarily - but I often do this wrapped in a debug define, so during debugging, the console is held open. During release, I'm usually not running inside VS, and when run from a command line, this is no longer an issue.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

The "don't do that" responses in this thread may seem curt, but they're fairly sensible. I happened across this question because I'm debugging a gtest suite that used to run fine, but now crashes mysteriously when launched. When run from the console, it pops up a dialog saying "blah.exe has stopped working"; but, when run from the debugger, the console pops up momentarily, vanishes, and the program exits with 0 status.

I should have been thinking about how odd this difference in behavior was, but instead I was like: "Aw, man---I gotta make that console window stay up so I can see what it says." Right?

It turns out that the machine I'm working on (a colleague of mine's) had the 'Command Arguments' for the debugger set to '--gtest_filter=*testMsg*'. I noticed this right away, too, but it never occurred to me that all the tests matching the filter had been renamed in a recent commit. So, when launched from the debugger, gtest wasn't finding any tests to run and was simply exiting. Another 90 minutes of my life I'll never get back. (-_-) I could have ditched this tar baby a lot sooner if my knee-jerk reaction hadn't been to think I needed that console window to stay open in order to solve the problem...

evadeflow
  • 4,704
  • 38
  • 51
0

Call this function before you return at the end of main:

void onEnd()
{
    printf("Press any key to exit...");
    _getch();
}
DeusAduro
  • 5,971
  • 5
  • 29
  • 36
0

You can specify that the executable is a Console app, not a Windows app, at least in VS2017. This makes your application run in a "Microsoft Visual Studio Debug Console", with "Press Any Key To Close This Window" appearing at the end of the console output:

Right click the project to bring up Properties.

Linker > System > Subsystem > Select "Console".

Press Apply before closing.

Big Al
  • 980
  • 1
  • 8
  • 20