csdnceshi69
YaoRaoLov
采纳率50%
2009-11-21 15:58 阅读 397

在 Visual Studio c / c + + 控制台应用上防止控制台窗口关闭

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?

转载于:https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

17条回答 默认 最新

  • 已采纳
    csdnceshi65 larry*wei 2009-11-21 16:00

    If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

    点赞 21 评论 复制链接分享
  • csdnceshi56 lrony* 2013-03-24 05:38

    Right click on your project

    Properties>Configuration Properties> Linker> System

    select Console (/SUBSYSTEM:CONSOLE) in SubSystem option.

    Now try it...it should work

    点赞 21 评论 复制链接分享
  • weixin_41568126 乱世@小熊 2009-11-28 20:13

    add “| pause” in command arguments box under debugging section at project properties.

    点赞 10 评论 复制链接分享
  • weixin_41568196 撒拉嘿哟木头 2013-04-04 17:34

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

    点赞 10 评论 复制链接分享
  • csdnceshi61 derek5. 2012-09-28 09:24

    Goto Debug Menu->Press StartWithoutDebugging

    点赞 9 评论 复制链接分享
  • csdnceshi74 7*4 2016-07-20 23:14

    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.

    点赞 7 评论 复制链接分享
  • csdnceshi51 旧行李 2009-11-21 17:30

    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.

    点赞 6 评论 复制链接分享
  • weixin_41568174 from.. 2017-10-23 20:37

    A somewhat better solution:

    atexit([] { system("PAUSE"); });
    

    at the beginning of your program.

    Pros:

    • can use std::exit()
    • can have multiple returns from main
    • you can run your program under the debugger
    • IDE independent (+ OS independent if you use the cin.sync(); cin.ignore(); trick instead of system("pause");)

    Cons:

    • have to modify code
    • won't pause on std::terminate()
    • will still happen in your program outside of the IDE/debugger session; you can prevent this under Windows using:

    extern "C" int __stdcall IsDebuggerPresent(void);
    int main(int argc, char** argv) {
        if (IsDebuggerPresent())
            atexit([] {system("PAUSE"); });
        ...
    }
    
    点赞 6 评论 复制链接分享
  • csdnceshi65 larry*wei 2014-10-15 14:35

    (/SUBSYSTEM:CONSOLE) did not worked for my vs2013 (I already had it).

    "run without debugging" is not an options, since I do not want to switch between debugging and seeing output.

    I ended with

    int main() {
      ...
    #if _DEBUG
      LOG_INFO("end, press key to close");
      getchar();
    #endif // _DEBUG
      return 0;
    }
    

    Solution used in qtcreator pre 2.6. Now while qt is growing, vs is going other way. As I remember, in vs2008 we did not need such tricks.

    点赞 5 评论 复制链接分享
  • csdnceshi57 perhaps? 2018-01-09 22:59

    just put as your last line of code:

    system("pause");
    
    点赞 5 评论 复制链接分享
  • csdnceshi51 旧行李 2018-04-14 23:08

    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.

    点赞 5 评论 复制链接分享
  • csdnceshi58 Didn"t forge 2009-11-21 16:00

    If you're using .NET, put Console.ReadLine() before the end of the program.

    It will wait for <ENTER>.

    点赞 4 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 2012-08-06 15:16

    Here is a way for C/C++:

    #include <stdlib.h>
    
    #ifdef _WIN32
        #define WINPAUSE system("pause")
    #endif
    

    Put this at the top of your program, and IF it is on a Windows system (#ifdef _WIN32), then it will create a macro called WINPAUSE. Whenever you want your program to pause, call WINPAUSE; and it will pause the program, using the DOS command. For other systems like Unix/Linux, the console should not quit on program exit anyway.

    点赞 2 评论 复制链接分享
  • weixin_41568131 10.24 2015-01-30 19:48

    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.

    点赞 1 评论 复制链接分享
  • csdnceshi65 larry*wei 2011-04-20 01:04

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

    点赞 评论 复制链接分享
  • weixin_41568208 北城已荒凉 2017-07-14 02:57

    Visual Studio 2015, with imports. Because I hate when code examples don't give the needed imports.

    #include <iostream>;
    
    int main()
    {
        getchar();
        return 0;
    }
    
    点赞 评论 复制链接分享
  • csdnceshi80 胖鸭 2018-10-17 05:37

    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;
    }
    
    点赞 评论 复制链接分享

相关推荐