user360907
user360907

Reputation:

Visual Studio terminates my console application too fast

When I execute my program in Visual Studio (just a simple hello world app) it terminates and closes the console window immediately, rather than waiting for me to close it manually. I have gotten round this by including cin.get() at the end of the program, but my instructor has just told me that I shouldn't have to do that, and that he was able to run the same program last night without having to enter the extra line.

Is this something in the setup of Visual Studio?


Update

I've tried using Ctrl+F5, but that just makes the console disappear even faster.

Upvotes: 1

Views: 7288

Answers (5)

radbyx
radbyx

Reputation: 9660

While testing Console.ReadKey() is nice to have. Here's an example:

namespace Foo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("The Console will close when it reads a key-input. "+
                          "Press a key to close");
            Console.ReadKey();
        }
    }
}

Upvotes: 0

Alan Simes
Alan Simes

Reputation: 201

This is by design and your instructor is incorrect. Try launching a .bat file from a folder view and you get precisely the same behaviour!

You can

  • Set a breakpoint

    Ask for user input via Console.Readline()

    Run to cursor

Upvotes: 4

Anup Singh
Anup Singh

Reputation: 323

This will work for sure: In old Visual studio 2010 and older, system properties were grandfathered from windows system, however 2010 and up this property was customized or set ti null. do the following and this will fix it for sure.

To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "*project_name* properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.

You are good to go NOW!!

Upvotes: 0

skelkingur
skelkingur

Reputation: 11

You can set a breakpoint and start the application in Debug mode. This way the IDE will halt at the breakpoint and the window doesn't get closed until you continue the execution of your code.

Upvotes: 1

Florian
Florian

Reputation: 390

Start in debug mode ;)

Upvotes: 1

Related Questions