Home > All, Hacks/Tricks, Programming > Prevent Your Console Window From Closing [C++ Programming]

Prevent Your Console Window From Closing [C++ Programming]

Introduction

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.

Not-So Solutions

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.

Advertisements
Categories: All, Hacks/Tricks, Programming
  1. Cócó
    Tuesday October 20, 2009 at 6:56 PM

    Thanks :)

  2. theambient
    Saturday November 28, 2009 at 4:09 PM

    I found solution for case of debugging too. Simply add “| pause” in command arguments box under debugging section at project properties.

  3. jt
    Monday January 18, 2010 at 6:18 PM

    Awesome tip; thanks a million!!

  4. Delight
    Sunday January 9, 2011 at 10:54 AM

    Thanks!

  1. No trackbacks yet.

Tell Me What You Think...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: