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 04:47

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

查看更多
何处买醉
3楼-- · 2018-12-31 04:51

just put as your last line of code:

system("pause");
查看更多
只若初见
4楼-- · 2018-12-31 04:52

Goto Debug Menu->Press StartWithoutDebugging

查看更多
还给你的自由
5楼-- · 2018-12-31 04:55

In my case, i experienced this when i created an Empty C++ project on VS 2017 community edition. You will need to set the Subsystem to "Console (/SUBSYSTEM:CONSOLE)" under Configuration Properties.

  1. Go to "View" then select "Property Manager"
  2. Right click on the project/solution and select "Property". This opens a Test property page
  3. Navigate to the linker then select "System"
  4. Click on "SubSystem" and a drop down appears
  5. Choose "Console (/SUBSYSTEM:CONSOLE)"
  6. Apply and save
  7. The next time you run your code with "CTRL +F5", you should see the output.
查看更多
泪湿衣
6楼-- · 2018-12-31 04:58

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.

查看更多
骚的不知所云
7楼-- · 2018-12-31 05:00

(/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.

查看更多
登录 后发表回答