How to keep the console window open in Visual C++?

Visual C++Console

Visual C++ Problem Overview


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?

Visual C++ Solutions


Solution 1 - Visual C++

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)

Solution 2 - Visual C++

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

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

Solution 3 - Visual C++

Put a breakpoint on the return line.

You are running it in the debugger, right?

Solution 4 - Visual C++

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

Solution 5 - Visual C++

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.

  1. Open your project file (.vcxproj) in a text editor.

  2. Inside the root <project> tag, insert the following:

> > > Console > >

  1. Reload the project in your solution.

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

Solution 6 - Visual C++

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

Solution 7 - Visual C++

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.

Solution 8 - Visual C++

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.

Solution 9 - Visual C++

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

Solution 10 - Visual C++

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.

Solution 11 - Visual C++

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

Solution 12 - Visual C++

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.

Solution 13 - Visual C++

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

Solution 14 - Visual C++

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

Solution 15 - Visual C++

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.

Solution 16 - Visual C++

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

Solution 17 - Visual C++

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

Solution 18 - Visual C++

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.

Solution 19 - Visual C++

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

Solution 20 - Visual C++

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.

Solution 21 - Visual C++

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.

Solution 22 - Visual C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRa&#250;l RoaView Question on Stackoverflow
Solution 1 - Visual C++ZoidbergView Answer on Stackoverflow
Solution 2 - Visual C++Gordon WilsonView Answer on Stackoverflow
Solution 3 - Visual C++Sam HarwellView Answer on Stackoverflow
Solution 4 - Visual C++Marcos MarinView Answer on Stackoverflow
Solution 5 - Visual C++JBentleyView Answer on Stackoverflow
Solution 6 - Visual C++Christian C. SalvadóView Answer on Stackoverflow
Solution 7 - Visual C++Juan CastanoView Answer on Stackoverflow
Solution 8 - Visual C++OdysView Answer on Stackoverflow
Solution 9 - Visual C++gonnavisView Answer on Stackoverflow
Solution 10 - Visual C++iko79View Answer on Stackoverflow
Solution 11 - Visual C++DenchikView Answer on Stackoverflow
Solution 12 - Visual C++ChadView Answer on Stackoverflow
Solution 13 - Visual C++Arnav BorborahView Answer on Stackoverflow
Solution 14 - Visual C++user6041670View Answer on Stackoverflow
Solution 15 - Visual C++Amruth PillaiView Answer on Stackoverflow
Solution 16 - Visual C++MartinView Answer on Stackoverflow
Solution 17 - Visual C++WelcorView Answer on Stackoverflow
Solution 18 - Visual C++Charaf ErrachidiView Answer on Stackoverflow
Solution 19 - Visual C++chen_767View Answer on Stackoverflow
Solution 20 - Visual C++user9416431View Answer on Stackoverflow
Solution 21 - Visual C++Jonathan LidbeckView Answer on Stackoverflow
Solution 22 - Visual C++Sifis BabionitakisView Answer on Stackoverflow