521

This is a probably an embarasing question as no doubt the answer is blindingly obvious.

I've used Visual Studio for years, but this is the first time I've done any 'Console Application' development.

When I run my application the console window pops up, the program output appears and then the window closes as the application exits.

Is there a way to either keep it open until I have checked the output, or view the results after the window has closed?

sorin
  • 161,544
  • 178
  • 535
  • 806
Martin
  • 39,569
  • 20
  • 99
  • 130

25 Answers25

487

If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

Tom
  • 20,852
  • 4
  • 42
  • 54
  • 276
    If you have a C++ app and Run Without Debugging and the console window still closes, you need to remember to explicitly set the Subsystem to Console under Configuration Properties / Linker / System. This can happen if you start with an Empty Project, which leaves Subsystem unset. – Trevor Robinson Aug 25 '10 at 18:39
  • 2
    You can also add the Start Without Debugging button to any toolbars/menus you want, if you root around in the `Tools->Customize...` dialog. – mwfearnley Oct 22 '13 at 17:02
  • 4
    Didn't work for me :( The console window opens and shuts immediately, but the log pauses and waits for an additional press of F5 – CodyBugstein Dec 08 '13 at 12:17
  • @lmray, did you set the subsystem as described in Trevor Robinson's comment on this answer? – Tom Dec 08 '13 at 14:52
  • 44
    The correct answer is the one by @TrevorRobinson: Here is the expanded version if you have trouble finding out where the config properties are: Right-click on your project in Visual Studio > Properties > Configuration Properties > Linker > System, change Subsystem to "Console" – Debajit Jan 09 '14 at 00:39
  • This is weird, if I run without debugging nothing happens at all. The app is not even started. It works fine with debugging. – Kokodoko Mar 29 '15 at 11:03
  • 1
    Console.ReadKey(true); will make your app stop and wait for a key to be pressed. – Dave Apr 21 '15 at 18:37
  • This doesn't work for Makefile projects. CTRL+F5 doesn't wait for input. – user2023370 Jan 03 '16 at 21:22
  • Hum... this stopped working in Visual Studio 2015 (at least as default)? – Pedro Reis Oct 10 '16 at 15:35
  • 1
    If you have set Subsystem:Console and Ctrl + F5 **STILL** results in a console window that instantly closes, you might want to double check that have the **Debug** configuration selected when making your changes. If you have another configuration selected, such as Release, when you set Subsystem:Console you will probably have trouble with a disappearing console after Ctrl + F5 is pressed. – Ry Lowry Mar 12 '17 at 23:43
  • 4
    Trevor Robinson is right. If you start a blank project, and C++ item. You need to go to View->Property Manager->Property(wrench icon)->Config. Properties->Linker->System->Subsystem->in the pop-down menu, choose Console. – most venerable sir Sep 20 '17 at 15:30
  • 11
    Im working on VS community 2017. `ctrl+f5` not working, there is no `Properties` when I right click project, also the `wrench icon` is greyed out. – ksha Dec 16 '17 at 06:54
255

Right click on your project

Properties > Configuration Properties > Linker > System

Select Console (/SUBSYSTEM:CONSOLE) in SubSystem option or you can just type Console in the text field!

Now try it...it should work

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Viraj
  • 2,567
  • 1
  • 12
  • 2
84

Starting from Visual Studio 2017 (15.9.4) there is an option:

Tools->Options->Debugging->Automatically close the console

The corresponding fragment from the Visual Studio documentation:

Automatically close the console when debugging stops:

Tells Visual Studio to close the console at the end of a debugging session.

Community
  • 1
  • 1
chronoxor
  • 3,159
  • 3
  • 20
  • 31
  • 7
    I just tested this in VS2017 and VS2019. It requires both setting the subsystem to CONSOLE and turning this option off. Having one or the other or neither closes the window automatically. – Casey Jul 27 '19 at 16:39
  • This still closes the window for me if I launch multiple projects from the same solution. – Serge Rogatch Aug 25 '19 at 11:40
  • I feel like this is the right answer for VS 2017 and forward. Too bad it's not in older versions. – All The Rage Dec 05 '19 at 02:51
  • 1
    How do I change the subsystem for a Makefile project? – André van Schoubroeck Dec 03 '20 at 15:04
  • 1
    I discovered that if the debugging a C++ console application where command arguments redirect a file to standard input (stdin), the window will automatically close regardless of the setting of this option. – RainMoose Apr 08 '22 at 17:57
  • The option is by default disabled for me in VS2022 community edition, but still console closes automatically. @Viraj answer worked for me. – Kartik Podugu Apr 13 '23 at 06:22
45

Here is a way for C/C++:

#include <stdlib.h>

#ifdef _WIN32
    #define WINPAUSE system("pause")
#endif

Put this at the top of your program, and IF it is on a Windows system (#ifdef _WIN32), then it will create a macro called WINPAUSE. Whenever you want your program to pause, call WINPAUSE; and it will pause the program, using the DOS command. For other systems like Unix/Linux, the console should not quit on program exit anyway.

carefulnow1
  • 803
  • 12
  • 30
Shaun
  • 515
  • 4
  • 2
35

Goto Debug Menu->Press StartWithoutDebugging

pashaplus
  • 3,596
  • 2
  • 26
  • 25
  • 1
    This isn't even an option in VS 2008 unless C# Development Environment was chosen. CTRL + F5 works though. You can also add this as a button to the toolbar via Tools > Customize. – perry Jan 07 '15 at 01:16
  • If you have multiple projects set the relevant one as the startup project – CAD bloke Mar 10 '15 at 04:09
33

If you're using .NET, put Console.ReadLine() before the end of the program.

It will wait for <ENTER>.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
16

try to call getchar() right before main() returns.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Magarusu
  • 982
  • 10
  • 14
  • Doesn't work in VS 2008 for a simple Hello Name console app. cout, cin, cout, getchar(), console still closes. What's the deal? – perry Jan 07 '15 at 01:11
  • 1
    Try putting getchar() twice if you have a cout just before getchar(). getchar() expects a char and you are already giving it with cout... Let me know if this helps :) – Magarusu Jan 08 '15 at 08:42
12

(/SUBSYSTEM:CONSOLE) did not worked for my vs2013 (I already had it).

"run without debugging" is not an options, since I do not want to switch between debugging and seeing output.

I ended with

int main() {
  ...
#if _DEBUG
  LOG_INFO("end, press key to close");
  getchar();
#endif // _DEBUG
  return 0;
}

Solution used in qtcreator pre 2.6. Now while qt is growing, vs is going other way. As I remember, in vs2008 we did not need such tricks.

Fantastory
  • 1,891
  • 21
  • 25
8

just put as your last line of code:

system("pause");
RafaelJan
  • 3,118
  • 1
  • 28
  • 46
7

Here's a solution that (1) doesn't require any code changes or breakpoints, and (2) pauses after program termination so that you can see everything that was printed. It will pause after either F5 or Ctrl+F5. The major downside is that on VS2013 Express (as tested), it doesn't load symbols, so debugging is very restricted.

  1. Create a batch file. I called mine runthenpause.bat, with the following contents:

    %1 %2 %3 %4 %5 %6 %7 %8 %9
    pause
    

    The first line will run whatever command you provide and up to eight arguments. The second line will... pause.

  2. Open the project properties | Configuration properties | Debugging.

  3. Change "Command Arguments" to $(TargetPath) (or whatever is in "Command").
  4. Change "Command" to the full path to runthenpause.bat.
  5. Hit OK.

Now, when you run, runthenpause.bat will launch your application, and after your application has terminated, will pause for you to see the console output.

I will post an update if I figure out how to get the symbols loaded. I tried /Z7 per this but without success.

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • 2
    This still doesn't support debugging, but at least it addresses the issue of not being able to see output e.g. of VLD, in contrast to **all** other answers. – Martin Hennings Feb 14 '17 at 10:26
4

add “| pause” in command arguments box under debugging section at project properties.

theambient
  • 95
  • 3
  • 4
    This would be a great solution if it worked. `pause` appears to only work in batch files, and while it receives input from your program on STDIN, it doesn't re-print that text. I tried replacing `|` with `&&`, but it had no effect -- presumably because it isn't executing with a command shell (cmd.exe). – Brian Vandenberg Aug 06 '12 at 18:49
3

You could run your executable from a command prompt. This way you could see all the output. Or, you could do something like this:

int a = 0;
scanf("%d",&a);

return YOUR_MAIN_CODE;

and this way the window would not close until you enter data for the a variable.

Geo
  • 93,257
  • 117
  • 344
  • 520
3

Just press CNTRL + F5 to open it in an external command line window (Visual Studio does not have control over it).

If this doesn't work then add the following to the end of your code:

Console.WriteLine("Press any key to exit...");
Console.ReadKey();

This wait for you to press a key to close the terminal window once the code has reached the end.

If you want to do this in multiple places, put the above code in a method (e.g. private void Pause()) and call Pause() whenever a program reaches a possible end.

carefulnow1
  • 803
  • 12
  • 30
2

A somewhat better solution:

atexit([] { system("PAUSE"); });

at the beginning of your program.

Pros:

  • can use std::exit()
  • can have multiple returns from main
  • you can run your program under the debugger
  • IDE independent (+ OS independent if you use the cin.sync(); cin.ignore(); trick instead of system("pause");)

Cons:

  • have to modify code
  • won't pause on std::terminate()
  • will still happen in your program outside of the IDE/debugger session; you can prevent this under Windows using:

extern "C" int __stdcall IsDebuggerPresent(void);
int main(int argc, char** argv) {
    if (IsDebuggerPresent())
        atexit([] {system("PAUSE"); });
    ...
}
GhassanPL
  • 2,679
  • 5
  • 32
  • 40
  • Can you elaborate why? – GhassanPL Jan 08 '18 at 10:08
  • In the case where one isn't debugging it does nothing useful, because in this case there is no problem to be solved. In VS just run the program with Ctrl+F5 and that's it, so there's no problem in VS, and in a command interpreter there's no problem, in short, there's no problem. And in the case where one is debugging and wants a stop added code to stop is not a more convenient solution than a breakpoint, and interferes in the situations where one doesn't want a stop. – Cheers and hth. - Alf Jan 08 '18 at 10:19
2

Either use:

  1. cin.get();

or

  1. system("pause");

Make sure to make either of them at the end of main() function and before the return statement.

AAEM
  • 1,837
  • 2
  • 18
  • 26
2

You can also use this option

#include <conio.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
   .
   .
   .
   getch(); 

   return 0;
}
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
2

In my case, i experienced this when i created an Empty C++ project on VS 2017 community edition. You will need to set the Subsystem to "Console (/SUBSYSTEM:CONSOLE)" under Configuration Properties.

  1. Go to "View" then select "Property Manager"
  2. Right click on the project/solution and select "Property". This opens a Test property page
  3. Navigate to the linker then select "System"
  4. Click on "SubSystem" and a drop down appears
  5. Choose "Console (/SUBSYSTEM:CONSOLE)"
  6. Apply and save
  7. The next time you run your code with "CTRL +F5", you should see the output.
0xsteve
  • 121
  • 1
  • 4
2

Sometimes a simple hack that doesnt alter your setup or code can be:

Set a breakpoint with F9, then execute Debug with F5.

user1323995
  • 1,286
  • 1
  • 10
  • 16
2

Since running it from VS attaches the VS debugger, you can check for an attached debugger:

if (Debugger.IsAttached)
{
    Console.WriteLine("Debugger is attached. Press any key to exit.");
    Console.ReadKey();
}

I guess the only caveat is that it'll still pause if you attach any other debugger, but that may even be a wanted behavior.

Juan
  • 15,274
  • 23
  • 105
  • 187
1

Use Console.ReadLine() at the end of the program. This will keep the window open until you press the Enter key. See https://learn.microsoft.com/en-us/dotnet/api/system.console.readline for details.

Caltor
  • 2,538
  • 1
  • 27
  • 55
Dallas
  • 43
  • 1
1

This is a detailed explanation for latest Visual Studio releases that worked for me:

  • Go to Solution Explorer by pressing CTRL + ALT + L, then right click on it and choose Properties. on the left side, double click Linker then choose System, then click on SubSystem and type CONSOLE, then click Apply.

  • Click on Tools then Options..., then go to the search bar (CTRL + E) and search for console then click Enter, then click on Debugging then General then disable Automatically close the console when debugging stops.

After that, press CTRL + F5 to start without debugging. and, Voilà!

iTzVoko
  • 135
  • 2
  • 10
0

Visual Studio 2015, with imports. Because I hate when code examples don't give the needed imports.

#include <iostream>;

int main()
{
    getchar();
    return 0;
}
KANJICODER
  • 3,611
  • 30
  • 17
0

Currently there is no way to do this with apps running in WSL2. However there are two work-arounds:

  1. The debug window retains the contents of the WSL shell window that closed.

  2. The window remains open if your application returns a non-zero return code, so you could return non-zero in debug builds for example.

0

It should be added that things have changed since then. On Windows 11 (probably 10, I can't check any more) the new Terminal app that now houses the various console, PowerShell and other sessions has its own settings regarding closing. Look for it in Settings > Defaults > Advanced > Profile termination behavior.

If it's set to close when a program exits with zero, then it will close, even if VS is told otherwise.

Gábor
  • 9,466
  • 3
  • 65
  • 79
-1

Go to Setting>Debug>Un-check close on end.

enter image description here

Priyanshu Singh
  • 704
  • 8
  • 12