Visual Studio terminates my console application to

2019-01-28 23:13发布

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.

5条回答
干净又极端
2楼-- · 2019-01-28 23:47

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!!

查看更多
甜甜的少女心
3楼-- · 2019-01-28 23:49

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

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-29 00:00

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.

查看更多
Luminary・发光体
5楼-- · 2019-01-29 00:07

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();
        }
    }
}
查看更多
闹够了就滚
6楼-- · 2019-01-29 00:08

Start in debug mode ;)

查看更多
登录 后发表回答