[visual-c++] How to keep the console window open in Visual C++?

I'm starting out in Visual C++ and I'd like to know how to keep the console window.

For instance this would be a typical "hello world" application:

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    return 0;
}

What's the line I'm missing?

This question is related to visual-c++ console

The answer is


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...


My 2 Cents:

Choice 1: Add a breakpoint at the end of main()

Choice 2: Add this code, right before the return 0;:

std::cout << "Press ENTER to continue..."; //So the User knows what to do
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

You need to include <iomanip> for std::numeric_limits


cin.get(), or system("PAUSE"). I haven't heard you can use return(0);


As some have already pointed out, Zoidbergs solution does not attach the debugger, which is something you usually don't want.

The best option imo is to configure your VS accordingly (from VS 2017 onwards), by going to Tools > Options > Debugging > General. There you uncheck "Automatically close the console when debugging stops" (at the very bottom), which is probably checked in your case.


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.


Another option:

#ifdef _WIN32
#define MAINRET system("pause");return 0
#else
#define MAINRET return 0
#endif

In main:

int main(int argc, char* argv[]) {
    MAINRET;
}

The standard way is cin.get() before your return statement.

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    cin.get();
    return 0;
}

You can use cin.get(); or cin.ignore(); just before your return statement to avoid the console window from closing.


Put a breakpoint on the return line.

You are running it in the debugger, right?


The standard way is cin.get() before your return statement.

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    cin.get();
    return 0;
}

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 /SUBSYSTEM:CONSOLE to the linker. - EDIT: see below.

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.


Simply add a Breakpoint to the closing bracket of your _tmain method. This is the easier way plus you don't have to add code in order to debug.


just add system("pause") at the end of the code before return 0 like this

#include <stdlib.h>

int main()
{
    //some code goes here
    system("pause")
    return 0;
}

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...


just add system("pause") at the end of the code before return 0 like this

#include <stdlib.h>

int main()
{
    //some code goes here
    system("pause")
    return 0;
}

You can use cin.get(); or cin.ignore(); just before your return statement to avoid the console window from closing.


cin.get(), or system("PAUSE"). I haven't heard you can use return(0);


Actually, the real solution is the selection of the project template itself. You MUST select Win32 Console Application in older VS, or fill in the project name first and then double click on Windows Desktop wizard and then select Win32 console application. Then select empty project at this point. This then allows for what the original questioner really wanted without adding extra stopping point and hold code. I went through this problem as well. The answer is also at MSDN site.


(Some options are may be called by different names. I do not use the english version)

I had the same problem, when I created projects with the option "empty project", Create project as "Win32-console application" instead of "empty project" . In the dialog which pops up now, you press "continue" and after that you may check the option "empty project" and press confirm. After that CTRL + F5 will open a console which does not close automatically.


You can use cin.get(); or cin.ignore(); just before your return statement to avoid the console window from closing.


The standard way is cin.get() before your return statement.

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    cin.get();
    return 0;
}

I include #include <conio.h> and then, add getch(); just before the return 0; line. That's what I learnt at school anyway. The methods mentioned above here are quite different I see.


Simply add a Breakpoint to the closing bracket of your _tmain method. This is the easier way plus you don't have to add code in order to debug.


I had the same problem; In my application there are multiple exit() points and there was no way to know where exactly it exits, then I found out about this:

atexit(system("pause"));

or

atexit(cin.get());

This way it'll stop no matter where we exit in the program.


You can use cin.get(); or cin.ignore(); just before your return statement to avoid the console window from closing.


Here's a way to keep the command window open regardless of how execution stops without modifying any code:

In Visual Studio, open Project Property Pages -> Debugging.

For Command, enter $(ComSpec)

For Command Arguments, enter /k $(TargetPath). Append any arguments to your own application.

Now F5 or Ctrl-F5 executes Windows/System32/cmd.exe in a new window, and /k ensures that the command prompt stays open after execution completes.

The downside is that execution won't stop on breakpoints.


int main()
{
    //...
    getchar();
    return 0;
}

you can just put keep_window_open (); before the return here is one example

int main()
{
    cout<<"hello world!\n";
    keep_window_open ();
    return 0;
}

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...


As some have already pointed out, Zoidbergs solution does not attach the debugger, which is something you usually don't want.

The best option imo is to configure your VS accordingly (from VS 2017 onwards), by going to Tools > Options > Debugging > General. There you uncheck "Automatically close the console when debugging stops" (at the very bottom), which is probably checked in your case.


Had the same problem. I am using _getch() just before the return statement. It works.


Put a breakpoint on the return line.

You are running it in the debugger, right?


Place a breakpoint on the ending brace of main(). It will get tripped even with multiple return statements. The only downside is that a call to exit() won't be caught.

If you're not debugging, follow the advice in Zoidberg's answer and start your program with Ctrl+F5 instead of just F5.


The standard way is cin.get() before your return statement.

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello World";
    cin.get();
    return 0;
}

I had the same problem; In my application there are multiple exit() points and there was no way to know where exactly it exits, then I found out about this:

atexit(system("pause"));

or

atexit(cin.get());

This way it'll stop no matter where we exit in the program.


Place a breakpoint on the ending brace of main(). It will get tripped even with multiple return statements. The only downside is that a call to exit() won't be caught.

If you're not debugging, follow the advice in Zoidberg's answer and start your program with Ctrl+F5 instead of just F5.


you can just put keep_window_open (); before the return here is one example

int main()
{
    cout<<"hello world!\n";
    keep_window_open ();
    return 0;
}

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 /SUBSYSTEM:CONSOLE to the linker. - EDIT: see below.

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.


Another option:

#ifdef _WIN32
#define MAINRET system("pause");return 0
#else
#define MAINRET return 0
#endif

In main:

int main(int argc, char* argv[]) {
    MAINRET;
}

Had the same problem. I am using _getch() just before the return statement. It works.


Actually, the real solution is the selection of the project template itself. You MUST select Win32 Console Application in older VS, or fill in the project name first and then double click on Windows Desktop wizard and then select Win32 console application. Then select empty project at this point. This then allows for what the original questioner really wanted without adding extra stopping point and hold code. I went through this problem as well. The answer is also at MSDN site.


I include #include <conio.h> and then, add getch(); just before the return 0; line. That's what I learnt at school anyway. The methods mentioned above here are quite different I see.


Here's a way to keep the command window open regardless of how execution stops without modifying any code:

In Visual Studio, open Project Property Pages -> Debugging.

For Command, enter $(ComSpec)

For Command Arguments, enter /k $(TargetPath). Append any arguments to your own application.

Now F5 or Ctrl-F5 executes Windows/System32/cmd.exe in a new window, and /k ensures that the command prompt stays open after execution completes.

The downside is that execution won't stop on breakpoints.


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.


My 2 Cents:

Choice 1: Add a breakpoint at the end of main()

Choice 2: Add this code, right before the return 0;:

std::cout << "Press ENTER to continue..."; //So the User knows what to do
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

You need to include <iomanip> for std::numeric_limits


int main()
{
    //...
    getchar();
    return 0;
}

(Some options are may be called by different names. I do not use the english version)

I had the same problem, when I created projects with the option "empty project", Create project as "Win32-console application" instead of "empty project" . In the dialog which pops up now, you press "continue" and after that you may check the option "empty project" and press confirm. After that CTRL + F5 will open a console which does not close automatically.