What is the Best Practice for Combating the Console Closing Issue?

前端 未结 10 1600
悲&欢浪女
悲&欢浪女 2020-11-27 22:21

After compiling console programs the console window closes immediately after running. What is the best practice for keeping it open? I\'ve searched google loads, I\'m used t

相关标签:
10条回答
  • 2020-11-27 22:58

    The "don't do that" responses in this thread may seem curt, but they're fairly sensible. I happened across this question because I'm debugging a gtest suite that used to run fine, but now crashes mysteriously when launched. When run from the console, it pops up a dialog saying "blah.exe has stopped working"; but, when run from the debugger, the console pops up momentarily, vanishes, and the program exits with 0 status.

    I should have been thinking about how odd this difference in behavior was, but instead I was like: "Aw, man---I gotta make that console window stay up so I can see what it says." Right?

    It turns out that the machine I'm working on (a colleague of mine's) had the 'Command Arguments' for the debugger set to '--gtest_filter=*testMsg*'. I noticed this right away, too, but it never occurred to me that all the tests matching the filter had been renamed in a recent commit. So, when launched from the debugger, gtest wasn't finding any tests to run and was simply exiting. Another 90 minutes of my life I'll never get back. (-_-) I could have ditched this tar baby a lot sooner if my knee-jerk reaction hadn't been to think I needed that console window to stay open in order to solve the problem...

    0 讨论(0)
  • 2020-11-27 23:00

    Since you always run in the debugger, set a breakpoint on the return statement from main().

    The debugger is your best friend, learn (and learn early) to use it to your advantage at every opportunity.

    0 讨论(0)
  • 2020-11-27 23:04

    A very common one is to just put in code to read a key from the console after your main application code closes. The keystroke read in just gets thrown away, but it holds the console open.

    It's not pretty, necessarily - but I often do this wrapped in a debug define, so during debugging, the console is held open. During release, I'm usually not running inside VS, and when run from a command line, this is no longer an issue.

    0 讨论(0)
  • 2020-11-27 23:06

    Call this function before you return at the end of main:

    void onEnd()
    {
        printf("Press any key to exit...");
        _getch();
    }
    
    0 讨论(0)
提交回复
热议问题