Keep Console Open With the Ctrl + F5 Shortcut in C# The best approach for keeping our console window open after the execution of code is to run it with the Ctrl + F5 shortcut of the Microsoft Visual Studio IDE.
Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.
system("PAUSE"); This will execute the windows command 'pause' by spawning a new cmd.exe/command.com process within your program.
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:
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)
The standard way is cin.get()
before your return statement.
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
cin.get();
return 0;
}
Put a breakpoint on the return
line.
You are running it in the debugger, right?
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...
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 - EDIT: see below./SUBSYSTEM:CONSOLE
to the linker.
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.
You can use cin.get();
or cin.ignore();
just before your return statement to avoid the console window from closing.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With