Preventing console window from closing on Visual S

2018-12-31 04:44发布

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?

18条回答
骚的不知所云
2楼-- · 2018-12-31 05:00

You can also use this option

#include <conio.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
   .
   .
   .
   getch(); 

   return 0;
}
查看更多
春风洒进眼中
3楼-- · 2018-12-31 05:01

Either use:

  1. cin.get();

or

  1. system("pause");

Make sure to make either of them at the end of main() function and before the return statement.

查看更多
宁负流年不负卿
4楼-- · 2018-12-31 05:04

try to call getchar() right before main() returns.

查看更多
伤终究还是伤i
5楼-- · 2018-12-31 05:04

Use console.readline. Yours is writing the line but not reading it.

查看更多
无与为乐者.
6楼-- · 2018-12-31 05:05

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.

查看更多
倾城一夜雪
7楼-- · 2018-12-31 05:06

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.

查看更多
登录 后发表回答