Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop console from closing on exit? [duplicate]

I'm using Visual Studio 2010 and Windows 7 x64

The command prompt closes after exit, even though I used "Start without debug". Is there a setting somewhere that I can use?

like image 657
segfault Avatar asked Nov 07 '10 14:11

segfault


People also ask

How do I stop my console from closing?

You can simply press Ctrl + F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.

Why is the console window closing immediately once displayed my output?

Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes.

How do I stop C++ console application from exiting immediately?

Before the end of your code, insert this line: system("pause"); This will keep the console until you hit a key. It also printed "Press any key to continue . . ." for me.


2 Answers

You can simply press Ctrl+F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.

However, if you use the above line, system("pause"); and press Ctrl+F5 to run, it will prompt you twice!

like image 63
Suhas Bharadwaj Avatar answered Sep 30 '22 19:09

Suhas Bharadwaj


Yes, in VS2010 they changed this behavior somewhy.
Open your project and navigate to the following menu: Project -> YourProjectName Properties -> Configuration Properties -> Linker -> System. There in the field SubSystem use the drop-down to select Console (/SUBSYSTEM:CONSOLE) and apply the change.
"Start without debugging" should do the right thing now.

Or, if you write in C++ or in C, put

system("pause"); 

at the end of your program, then you'll get "Press any key to continue..." even when running in debug mode.

like image 33
lapis Avatar answered Sep 30 '22 18:09

lapis