Prevent Your Console Window From Closing [C++ Programming]
First off, I’ll state that this solution has only been tested on Microsoft Visual Studio 2008 while writing a Visual C++ program. I don’t know how well it will work with other platforms, or in other situations. With that being said, let’s get into a description of the problem, and the solution.
When writing programs, it’s essential to run them in order to check for problems (debugging). If it’s a console application, a console window will pop up in which your program will run.
Let’s say, using a simple example, you’re writing the legendary “Hello World” application. You write your program, and then you run it. The problem arises when you run the program. You barely catch a glimpse of the window displaying “Hello World” when it immediately closes.
There have been several solutions offered online. Usually, these solutions involve adding lines of code to your program, such as cin.get(), system(“pause”), or something similar. These solutions are rather cumbersome and unnecessary when taking into account that your program is not meant to be run from a developer environment. The end user isn’t going to need that type of code in the program. It is unnecessary and inefficient.
The Real Solution
Surprisingly, the fix for this problem is very simple. I found that when I ran my programs, I always hit the “Play” button on the Debug toolbar. That green arrow actually means “Start Debugging.”
If you press Ctrl+F5, or, in the “Debug” menu click “Start Without Debugging” when you want to run your program, the console window will not automatically close after your program is finished. You will get the “Press Any Key To Continue…” message. Voila!!
Problem solved. I hope this helps!
Read a book. It’s good for you.