在 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

csdnceshi54
hurriedly% Monodevelop does what you are expecting.
接近 4 年之前 回复

17个回答

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.

csdnceshi75
衫裤跑路 Im working on VS community 2017. ctrl+f5 not working, there is no Properties when I right click project, also the wrench icon is greyed out.
接近 3 年之前 回复
csdnceshi79
python小菜 Trevor Robinson is right. If you start a blank project, and C++ item. You need to go to View->Property Manager->Property(wrench icon)->Config. Properties->Linker->System->Subsystem->in the pop-down menu, choose Console.
大约 3 年之前 回复
csdnceshi76
斗士狗 If you have set Subsystem:Console and Ctrl + F5 STILL results in a console window that instantly closes, you might want to double check that have the Debug configuration selected when making your changes. If you have another configuration selected, such as Release, when you set Subsystem:Console you will probably have trouble with a disappearing console after Ctrl + F5 is pressed.
3 年多之前 回复
csdnceshi60
℡Wang Yan Hum... this stopped working in Visual Studio 2015 (at least as default)?
大约 4 年之前 回复
csdnceshi56
lrony* This doesn't work for Makefile projects. CTRL+F5 doesn't wait for input.
接近 5 年之前 回复
csdnceshi59
ℙℕℤℝ This works in VS 2015
大约 5 年之前 回复
csdnceshi52
妄徒之命 Console.ReadKey(true); will make your app stop and wait for a key to be pressed.
5 年多之前 回复
csdnceshi57
perhaps? This is weird, if I run without debugging nothing happens at all. The app is not even started. It works fine with debugging.
5 年多之前 回复
csdnceshi51
旧行李 The correct answer is the one by @TrevorRobinson: Here is the expanded version if you have trouble finding out where the config properties are: Right-click on your project in Visual Studio > Properties > Configuration Properties > Linker > System, change Subsystem to "Console"
接近 7 年之前 回复
csdnceshi54
hurriedly% did you set the subsystem as described in Trevor Robinson's comment on this answer?
接近 7 年之前 回复
weixin_41568184
叼花硬汉 Didn't work for me :( The console window opens and shuts immediately, but the log pauses and waits for an additional press of F5
接近 7 年之前 回复
csdnceshi67
bug^君 You can also add the Start Without Debugging button to any toolbars/menus you want, if you root around in the Tools->Customize... dialog.
大约 7 年之前 回复
csdnceshi80
胖鸭 If you have a C++ app and Run Without Debugging and the console window still closes, you need to remember to explicitly set the Subsystem to Console under Configuration Properties / Linker / System. This can happen if you start with an Empty Project, which leaves Subsystem unset.
大约 10 年之前 回复

Right click on your project

Properties>Configuration Properties> Linker> System

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

Now try it...it should work

csdnceshi67
bug^君 this is the real solution ++
大约 3 年之前 回复
csdnceshi54
hurriedly% as @JonathanMee stated you still have run Ctrl + F5. This was already set in my visual studio 2015, so the first answer was the one that helped most.
3 年多之前 回复
weixin_41568134
MAO-EYE You have to run with Ctrl + F5 after setting this, that's the kicker.
大约 4 年之前 回复
csdnceshi60
℡Wang Yan This should be the accepted answer included with the current accepted answer.
4 年多之前 回复
weixin_41568131
10.24 This worked for my VS2013 Express, but the accepted answer did not. Very strange, as the accepted answer has worked for all Visual Studio versions I've used in the past.
5 年多之前 回复

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

It will wait for <ENTER>.

csdnceshi58
Didn"t forge Then your code is most likely exiting before it reaches that point.
接近 4 年之前 回复
csdnceshi80
胖鸭 Weird, this has no effect in my app. It still closes without waiting for user input.
5 年多之前 回复
csdnceshi75
衫裤跑路 -1, modifying code to work around the IDE
大约 6 年之前 回复
weixin_41568184
叼花硬汉 +1 - Either that or Console.ReadKey() to just wait for any key.
接近 11 年之前 回复

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

csdnceshi63
elliott.david It does work in VS 2015 though.
接近 4 年之前 回复

just put as your last line of code:

system("pause");

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.

csdnceshi53
Lotus@ There is no need to create the macro--just call system("pause"). Surround it with #ifdef if you must use it on other systems.
大约 3 年之前 回复
weixin_41568196
撒拉嘿哟木头 Worked for me on Visual Studio 2013 Express. :)
大约 4 年之前 回复
csdnceshi74
7*4 Don't do that, please. The pause command is Windows-specific.
大约 4 年之前 回复
weixin_41568183
零零乙 I did not include any library or namespace but it run correctly, thanks a lot.
4 年多之前 回复
csdnceshi69
YaoRaoLov You have to include the windows.h header
接近 5 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 This method does not work in Visual C++ (unless you include some header?)
接近 5 年之前 回复
csdnceshi61
derek5. I like this method the best.
接近 5 年之前 回复

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"); });
    ...
}
csdnceshi72
谁还没个明天 In the case where one isn't debugging it does nothing useful, because in this case there is no problem to be solved. In VS just run the program with Ctrl+F5 and that's it, so there's no problem in VS, and in a command interpreter there's no problem, in short, there's no problem. And in the case where one is debugging and wants a stop added code to stop is not a more convenient solution than a breakpoint, and interferes in the situations where one doesn't want a stop.
接近 3 年之前 回复
csdnceshi62
csdnceshi62 Can you elaborate why?
接近 3 年之前 回复
csdnceshi72
谁还没个明天 Very bad advice.
接近 3 年之前 回复

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

csdnceshi59
ℙℕℤℝ This would be a great solution if it worked. pause appears to only work in batch files, and while it receives input from your program on STDIN, it doesn't re-print that text. I tried replacing | with &&, but it had no effect -- presumably because it isn't executing with a command shell (cmd.exe).
大约 8 年之前 回复

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.

weixin_41568126
乱世@小熊 When I hit Ctrl + F5, nothing happens. It used to work for me but all on a sudden, it stopped working.
2 年多之前 回复
csdnceshi61
derek5. The second solution was what I was looking for!
3 年多之前 回复
weixin_41568183
零零乙 Read my edit...
4 年多之前 回复
csdnceshi65
larry*wei when i do the cntrl+f5 it opens then closes immediately? how do you fix that?
4 年多之前 回复

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.

共17条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐