C++ Console applications exit immediately when run

    General discussion

  • A lot of users have been asking why their console application terminates immediately after running/debugging their program in the Visual C++ IDE.  Users claim to see a quick flash of the console application and then it disappears.

    This is due to the fact that when your program is run, it creates a temporary console session to invoke your application.  Without any code to explicitly keep the console open (i.e. requiring that the user enter in a value using cin >>), the console terminates immediately after the code completes.  This is why you see a quick flash of your console application.

    There are a few things you can do to see the output of your program.

    1.  Start your application without debugging (Ctrl+F5).  When doing this, the C++ IDE creates a batch file that executes your code and displays the text "Press any key to continue".  This will allow the console to stay on the screen and allow you to view the output.
    2. Set a breakpoint in the code and run with debugging (F5).  When you hit the breakpoint, you can see the console window's current output.
    3. Run the application from a command prompt.  Press Start->All Programs->Visual C++ 2005 Express Edition->Visual Studio Tools->Visual Studio 2005 command prompt.  Then navigate to the directory (using 'cd') where the .exe is located.  Run the application by typing in the name of the .exe at the command prompt.  You will see the output from the program in the command prompt.

    Hopefully this will help people get their first C++ application up and running!




    Thursday, November 02, 2006 1:32 AM

All replies

  • How about a better method, instead of going through all of that trouble simply put:

    system ("pause");

    so the skeleton of a basic program would now look like:

    #include <iostream>
    using namespace std;

    int main()
           system ("pause");
           return 0;
    Saturday, November 11, 2006 4:52 AM
  • in .NET, you can use:


    Sunday, November 12, 2006 10:00 PM
  • Noob question:

    In managed mode I can use System::ReadLine(), ok.

    For unmanaged mode I could use "cin >> " but I'd like to wait only the enter key and
    don't want to introduce values... How can I do it?

    Monday, November 13, 2006 8:25 AM
  • The more commonly used (as far as I know) and more efficient way is to use cin.get() command.
    Wednesday, November 22, 2006 3:08 PM
  • system("pause") is probably the best way.

    cin.get(); sometimes fails to pause the program especially if you program has other cin statements just before cin.get();
    Tuesday, November 28, 2006 12:06 PM
  • No, system("pause") is certainly *not* the best way. The console application is doing exactly what it should. I.e. output some text, then exit (due to return 0;). A console application is *supposed* to be run through a *console*, as strange as it may seem.
    That way, when you run the application, you see its output, then it exits, and brings you back to the shell, waiting for your next command.
    Saturday, December 02, 2006 11:34 PM
  • #include <iostream> 
    #include <conio.h> 
    using namespace std; 
    int main() 
       cout << "Your body of code\n"
       //Desired pause location 
       cout << "The rest of your code\n"
       return 0; 

    cin.get() can often be difficult to get to work correctly, specifically if std::cin has been used previously in the body of code.  The easiest way to pause any c++ console application is to use the above snippet.  Note the inclusion of <conio.h>.  Also note that _getch() should be used in place of getch() in order to remain ISO conformant.

    Finally, notice how the pause created by _getch() is terminated by any keypress, making it very efficient for the user at runtime.
    Monday, February 16, 2009 5:18 PM
  • None of these steps work in Visual Studio 10. Has any one had an luck getting the system to pause in VS 10?
    Sunday, September 13, 2009 7:45 PM
  • when i run my "hello world" project it says the project is out of date..blabla

    and converts it but never pauses....using visual c++ 2008

    Monday, September 14, 2009 8:31 PM
  • #if _DEBUG

    Wednesday, December 16, 2009 11:04 AM
  • I have gotten the Pause to work just fine but I see something tricky in the #if someone explaine this to me please!

    I am going to paste in a small piece of code and teh problem i see is that when I run my app from VS2010 with the Debugger Attached either the Green play button or (F5) this block of code does not execute.

    # if _debug

    If I run this app without the debugger attached (ctrl+F5) then I get only one line of the Pause which is the code that the IDE supplies so again my block of code does not run.


    If i change the block of code to the following. When I run the app via green play button or (F5) The block of code executes. if i run with (ctrl+F5) then it executes also.

    # if ! _debug

    I get the same results with _getch();

    Now I am new to C++ I have been developing in the .Net framework for several years but most of the time it was been with VB and C#. So it could be something very simple I would just like an explenation. 


    Monday, May 03, 2010 7:36 PM
  • It is case sensitive, so it should be

    #ifdef _DEBUG



    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Monday, May 03, 2010 9:47 PM
  • Thanks... But why does it compile if it is Case sensitive? That would mean that there is no Object with that name yet it still compiles.. I do miss the intellisence of VB at this moment.. Reminds me of college when I had to write Java in notepad... Seems that we are not leveraging the tools of the IDE.

    Tuesday, May 04, 2010 4:54 AM
  • The #if statement is the preprocessor, so it is looking for if the symbol exists.

    When you write

    #if _DEBUG

    You are saying to the preprocessor, if this symbol exists and it has a non zero value. So this means that undefined symbols are treated as zero. But I think the biggest thing to remember is the preprocessor isn't part of the main compilation. Whats more, it isn't seen as an error since this is how conditional compilation is done. For example, we have types like size_t which change size depending on platform, under 32 bit Windows it is seen as unsigned int, on 64 bit Windows it is seen as long long. This could be accomplished by the #if statement as follows

    #ifdef _WIN64
    typedef long long size_t;
    typedef unsigned int size_t;

    So on the 32 bit compiler where _WIN64 isn't defined, it will remove the first typedef and see the second one only. On a 64 bit compiler where _WIN64 is defined it will use the first typedef and remove the second. So remember #if is different from if, and it is not an error when a symbol used by #if is not defined.

    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Tuesday, May 04, 2010 3:21 PM
  • Ok I get it now.. Thanks now I feel like an idiot for not knowing that. Guess I still have lots to learn. the main thing here was that the #if . So as it turns out I should have been using #ifdef as I have learned the diff between #if and #ifdef. But like I said Thanks again I appreciate your time.

    Wednesday, May 05, 2010 3:40 AM
  • //Another better way to pause the console.
    #include <iostream>
    inline void pause();
    int main()
    	int a, b, ans;
    	std::cout << "Input a: ";
    	std::cin >> a;
    	std::cout << std::endl <<"Input b: ";
    	std::cin >> b;
    	ans = a + b;
    	std::cout << a << "+" << b << "=" << ans << std::endl;
    	return 0;
    void pause()
    	std::cin.ignore();//Ignores previous input, get cin.get working even with new line inputs.

    Thursday, June 17, 2010 9:14 AM
  • You can also try this


    #include <iostream>

    using std::cin;

    int main()


    int i=0;

    //your rest of codes go here

    cin >> i;


    Tuesday, July 06, 2010 12:51 PM
  • As previously mentioned do one of the below:

    1. Run the application with CTRL+F5
    2. Insert a
      at the end of you program, such as last line in Main().
    Thursday, September 02, 2010 3:02 PM
  • Here is another reason the console may disappear and the solution.

    With the new visual studio 2010 you might see this behavior even when you use ctrl f5 aka "start without debugging". This is most likely because you created an "empty project" instead of a "Win32 console application". If you create the project as a "Win32 console application" you can disregard this as it does not apply.

    In the older versions it would default to the console subsystem even if you selected "empty project", but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "*project_name* properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.
    Friday, January 21, 2011 5:12 AM
  • I've never heard of this before.  This is great!  Works great!  GREAT!  :)

    EDIT: _getch() from conio.h, that is.

    Tuesday, February 15, 2011 4:01 AM
  • Great! It works.. thanks..;
    Sunday, August 28, 2011 11:22 PM
  • The best here, cheers
    Tuesday, October 04, 2011 7:27 PM
  • Thank y Micro Hard ou for the above method , it took me a while to figure out that i had to double click "linker" and "system" etc to display extra properties as i am new to programming , but your solution works without the need to add any extra code .Thank you 
    Monday, August 06, 2012 12:10 PM