Question

I'm writing C++ console programs. After compilation, when I run the program from my file browser, cmd.exe automatically closes such that I can't see my programs output.

The only way to work around this I've found is to run the program from inside cmd.exe

Is there anyway to keep cmd.exe open after a program finishes running?

Is there a setting I can change somewhere? I don't want to run a batch script with cmd.exe /K

Thanks!

[Edit] Don't know if this matters, but I'm on Vista x64

Was it helpful?

Solution

Have your application ask for a keypress before exiting - that's the easiest fix!

OTHER TIPS

You can setup a shortcut with the /K switch when launching cmd.exe to have it not terminate after running a given command:

 cmd.exe /K YourProgram.exe

I've always been a fan of just creating a batch file that calls you're program and then calls pause

Prog.exe Pause

This will give a nice "Press any key to continue..." prompt, it's simple and doesn't require modification of program.

As the last line of your main() function, you can add this line:

system("PAUSE");

Also, make sure to #include <stdlib.h> to declare the system() function. This will make the console pause. However, if your program is run from inside cmd.exe, this will still pause, which may be undesirable.

I know you asked for how to do it via a file browsers, but in case other people are interested in the same problem but through visual studio:

It's best to set a breakpoint right before your program ends.

Then you can deploy your exe and you can be sure that you won't forget to remove the asking for input. It's also better then asking for input because it takes a lot of time to comment out and back in the asking for input.

I think it is best not to ask for input and to instead start your program from a launched command prompt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top