views:

1197

answers:

5

This is a probably an embarasing question as no doubt the answer is blindingly obvious.

I've used Visual Studio for years, but this is the first time I've done any 'Console Application' development.

When I run my application the console window pops up, the program output appears and then the window closes as the application exits.

Is there a way to either keep it open until I have checked the output, or view the results after the window has closed?

+8  A: 

If you're using .NET, put Console.ReadLine() before the end of the program.

It will wait for <ENTER>.

Cheeso
+1 - Either that or `Console.ReadKey()` to just wait for any key.
Fredrik Mörk
Unfortunately not using .Net :(
Martin
+8  A: 

If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

Tom
Thanks Tom - that worked perfectly.
Martin
If you have a C++ app and Run Without Debugging and the console window still closes, you need to remember to explicitly set the Subsystem to Console under Configuration Properties / Linker / System. This can happen if you start with an Empty Project, which leaves Subsystem unset.
Trevor Robinson
+8  A: 

Throw a Console.ReadKey() at the end of your Main() method to wait for a keypress. If you want to be really user-friendly, HCI and all that, you can put Console.WriteLine("Press any key to exit") before that.

Dale Halliwell
+1  A: 

You could run your executable from a command prompt. This way you could see all the output. Or, you could do something like this:

int a = 0;
scanf("%d",&a);

return YOUR_MAIN_CODE;

and this way the window would not close until you enter data for the a variable.

Geo
+3  A: 

add “| pause” in command arguments box under debugging section at project properties.

theambient