可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I\'m starting out in Visual C++ and I\'d like to know how to keep the console window.
For instance this would be a typical \"hello world\" application:
int _tmain(int argc, _TCHAR* argv[])
{
cout << \"Hello World\";
return 0;
}
What\'s the line I\'m missing?
回答1:
Start the project with Ctrl+F5 instead of just F5.
The console window will now stay open with the Press any key to continue . . .
message after the program exits.
Note that this requires the Console (/SUBSYSTEM:CONSOLE)
linker option, which you can enable as follows:
- Open up your project, and go to the Solution Explorer. If you\'re following along with me in K&R, your \"Solution\" will be \'hello\' with 1 project under it, also \'hello\' in bold.
- Right click on the \'hello\" (or whatever your project name is.)
- Choose \"Properties\" from the context menu.
- Choose Configuration Properties>Linker>System.
- For the \"Subsystem\" property in the right-hand pane, click the drop-down box in the right hand column.
- Choose \"Console (/SUBSYSTEM:CONSOLE)\"
- Click Apply, wait for it to finish doing whatever it does, then click OK. (If \"Apply\" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won\'t work.)
CTRL-F5 and the subsystem hints work together; they are not separate options.
(Courtesy of DJMorreTX from http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6)
回答2:
The standard way is cin.get()
before your return statement.
int _tmain(int argc, _TCHAR* argv[])
{
cout << \"Hello World\";
cin.get();
return 0;
}
回答3:
Put a breakpoint on the return
line.
You are running it in the debugger, right?
回答4:
Another option is to use
#include <process.h>
system(\"pause\");
Though this is not very portable because it will only work on Windows, but it will automatically print
Press any key to continue...
回答5:
For makefile projects, the accepted solution fails, due to a bug in Visual Studio (which is present at least up until version 2012 - I haven\'t yet tested 2013). This bug is detailed here.
In order to have the console pause after program termination on a makefile project, perform these steps (this may differ for versions other than 2010 - 2012):
1) Pass /SUBSYSTEM:CONSOLE
to the linker. - EDIT: see below.
2) Open your project file (.vcxproj) in a text editor.
3) Inside the root <project>
tag, insert the following:
<ItemDefinitionGroup>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
4) Reload the project in your solution.
5) Run the program without debugging (CTRL + F5).
EDIT:
As per my comment below, setting the linker option /SUBSYSTEM:CONSOLE
is actually irrelevant for makefile projects (and not necessarily even possible, if you are using a compiler other than MSVC). All that matters is that the setting is added to the .vcxproj file, as per step 3 above.
回答6:
You can use cin.get();
or cin.ignore();
just before your return statement to avoid the console window from closing.
回答7:
just put a breakpoint on the last curly bracket of main.
int main () {
//...your code...
return 0;
} //<- breakpoint here
it works for me, no need to run without debugging. It also executes destructors before hitting the breakpoint so you can check any messages print on these destructors if you have any.
回答8:
Simply add a Breakpoint to the closing bracket of your _tmain
method. This is the easier way plus you don\'t have to add code in order to debug.
回答9:
Place a breakpoint on the ending brace of main()
. It will get tripped even with multiple return
statements. The only downside is that a call to exit()
won\'t be caught.
If you\'re not debugging, follow the advice in Zoidberg\'s answer and start your program with Ctrl+F5 instead of just F5.
回答10:
cin.get(), or system(\"PAUSE\").
I haven\'t heard you can use return(0);
回答11:
I include #include <conio.h>
and then, add getch();
just before the return 0;
line. That\'s what I learnt at school anyway. The methods mentioned above here are quite different I see.
回答12:
Had the same problem . I am using _getch() just before return statement. It works.
回答13:
I had the same problem; In my application there are multiple exit() points and there was no way to know where exactly it exits, then I found out about this:
atexit(system(\"pause\"));
or
atexit(cin.get());
This way it\'ll stop no matter where we exit in the program.
回答14:
My 2 Cents:
Choice 1: Add a breakpoint at the end of main()
Choice 2: Add this code, right before the return 0;
:
std::cout << \"Press ENTER to continue...\"; //So the User knows what to do
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), \'\\n\');
You need to include <iomanip>
for std::numeric_limits
回答15:
(Some options are may be called by different names. I do not use the english version)
I had the same problem, when I created projects with the option \"empty project\", Create project as \"Win32-console application\" instead of \"empty project\" . In the dialog which pops up now, you press \"continue\" and after that you may check the option \"empty project\" and press confirm. After that CTRL + F5 will open a console which does not close automatically.
回答16:
you can just put
keep_window_open ();
before the return here is one example
int main()
{
cout<<\"hello world!\\n\";
keep_window_open ();
return 0;
}
回答17:
Another option:
#ifdef _WIN32
#define MAINRET system(\"pause\");return 0
#else
#define MAINRET return 0
#endif
In main:
int main(int argc, char* argv[]) {
MAINRET;
}
回答18:
just add system(\"pause\") at the end of the code before return 0 like this
int main()
{
some code goes here
system(\"pause\")
return 0;
}
回答19:
Actually, the real solution is the selection of the project template itself.
You MUST select Win32 Console Application in older VS, or fill in the project name first and then double click on Windows Desktop wizard and then select Win32 console application. Then select empty project at this point. This then allows for what the original questioner really wanted without adding extra stopping point and hold code.
I went through this problem as well. The answer is also at MSDN site.
回答20:
Here\'s a way to keep the command window open regardless of how execution stops without modifying any code:
In Visual Studio, open Project Property Pages -> Debugging.
For Command, enter $(ComSpec)
For Command Arguments, enter /k $(TargetPath)
. Append any arguments to your own application.
Now F5 or Ctrl-F5 executes Windows/System32/cmd.exe in a new window, and /k ensures that the command prompt stays open after execution completes.
The downside is that execution won\'t stop on breakpoints.