199
votes

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?

22
Amruth A. Pillai your code doesn't show "press any key to continue" thanksuser2313493
You can print that yourself with a simple std::cout call.Raúl Roa
The downside to all proposed solutions is that none of them work with debugging (Ctrl+F5 fails here) and when the application stops unexpectedly (all breakpoints or reads from stdin at main return fail here). What I would love to see is an in-IDE console window like Eclipse and other IDEs have. They simply keep showing the output to stdout/stderr after the program has terminated.dr. Sybren
@sybren The accepted answer works with CTRL+F5, and why would you want a solution which works with debugging (F5)? Surely the whole point of debugging is to .. debug? What is the benefit of having a paused console after program termination, in a debugging session?JBentley
@JBentley Eclipse and other IDEs allow you read your program's output even after the process was terminated. Surely you see the added benefit, especially when trying to find a bug? Also a breakpoint only works when you know where the program is terminating, which can be hard to tell when the output disappears from your screen.dr. Sybren

22 Answers

399
votes

Start the project with Ctrl+F5 instead of just F5.

The console window will now stay open with the Press any key to continue . . . message after the program exits.

Note that this requires the Console (/SUBSYSTEM:CONSOLE) linker option, which you can enable as follows:

  1. Open up your project, and go to the Solution Explorer. If you're following along with me in K&R, your "Solution" will be 'hello' with 1 project under it, also 'hello' in bold.
  2. Right click on the 'hello" (or whatever your project name is.)
  3. Choose "Properties" from the context menu.
  4. Choose Configuration Properties>Linker>System.
  5. For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column.
  6. Choose "Console (/SUBSYSTEM:CONSOLE)"
  7. Click Apply, wait for it to finish doing whatever it does, then click OK. (If "Apply" is grayed out, choose some other subsystem option, click Apply, then go back and apply the console option. My experience is that OK by itself won't work.)

CTRL-F5 and the subsystem hints work together; they are not separate options.

(Courtesy of DJMorreTX from http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/21073093-516c-49d2-81c7-d960f6dc2ac6)

41
votes

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

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

Put a breakpoint on the return line.

You are running it in the debugger, right?

18
votes

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

8
votes

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.

4
votes

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

4
votes

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.

3
votes

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.

2
votes

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.

2
votes

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

2
votes

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;
}
2
votes
int main()
{
    //...
    getchar();
    return 0;
}
2
votes

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

2
votes

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.

1
votes

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.

1
votes

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

0
votes

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

0
votes

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.

0
votes

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;
}
0
votes

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.

0
votes

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.

-1
votes

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;
}