Preventing console window from closing on Visual Studio C/C++ Console application

前端 未结 21 1690
灰色年华
灰色年华 2020-11-21 23:42

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

相关标签:
21条回答
  • 2020-11-22 00:03

    Starting from Visual Studio 2017 (15.9.4) there is an option:

    Tools->Options->Debugging->Automatically close the console
    

    The corresponding fragment from the Visual Studio documentation:

    Automatically close the console when debugging stops:

    Tells Visual Studio to close the console at the end of a debugging session.

    0 讨论(0)
  • 2020-11-22 00:03

    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.

    0 讨论(0)
  • 2020-11-22 00:05

    Use Console.ReadLine() at the end of the program. This will keep the window open until you press the Enter key. See https://docs.microsoft.com/en-us/dotnet/api/system.console.readline for details.

    0 讨论(0)
  • 2020-11-22 00:06

    just put as your last line of code:

    system("pause");
    
    0 讨论(0)
  • 2020-11-22 00:07

    Here's a solution that (1) doesn't require any code changes or breakpoints, and (2) pauses after program termination so that you can see everything that was printed. It will pause after either F5 or Ctrl+F5. The major downside is that on VS2013 Express (as tested), it doesn't load symbols, so debugging is very restricted.

    1. Create a batch file. I called mine runthenpause.bat, with the following contents:

      %1 %2 %3 %4 %5 %6 %7 %8 %9
      pause
      

      The first line will run whatever command you provide and up to eight arguments. The second line will... pause.

    2. Open the project properties | Configuration properties | Debugging.

    3. Change "Command Arguments" to $(TargetPath) (or whatever is in "Command").
    4. Change "Command" to the full path to runthenpause.bat.
    5. Hit OK.

    Now, when you run, runthenpause.bat will launch your application, and after your application has terminated, will pause for you to see the console output.

    I will post an update if I figure out how to get the symbols loaded. I tried /Z7 per this but without success.

    0 讨论(0)
  • 2020-11-22 00:07

    Just press CNTRL + F5 to open it in an external command line window (Visual Studio does not have control over it).

    If this doesn't work then add the following to the end of your code:

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
    

    This wait for you to press a key to close the terminal window once the code has reached the end.

    If you want to do this in multiple places, put the above code in a method (e.g. private void Pause()) and call Pause() whenever a program reaches a possible end.

    0 讨论(0)
提交回复
热议问题