Reputation: 12396
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?
Upvotes: 212
Views: 316725
Reputation: 1258
As some have already pointed out, Zoidberg's 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.
edit:
adding Wheezil's additional info here (thanks):
make sure you build for Console subsystem by specifying /SUBSYSTEM:CONSOLE
(Project Properties > Linker > System > SubSystem)
Upvotes: 12
Reputation: 41
It has been mentioned individually, but somehow nobody spoke it out clearly:
cin.ignore();
cin.get();
return 0;
That is what you want, if you want your Console Window stay open at the end of e.g. a school test program for learning C++, which was 100% what has been asked for.
With only cin.get(); the console will still close if there was input (remainings in the input stream) and cin.ignore(); will exactly make sure, that previous input is not penetrating. With only cin.ignore(); nothing has been achieved, obviously and if you use cin.get(); before cin.ignore(), nothing is the result too, because nothing remains to wait for new input, since all have been satisfied by the existing input stream before ignore.
Hopefully it helps someone, that starts freshly and want to understand how to keep the console window open, until the next explicit input, without using anything legacy or special. And hopefully it doesn't hurt the specialists too much, so no one get hurt and all can be happy.
Upvotes: 0
Reputation: 1
Just after your includes YW std::cin.clear(); // reset any error flags std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n'); // ignore any characters in the input buffer until we find an enter character std::cin.get(); // get one more char from the user
Upvotes: 0
Reputation: 4196
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:
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)
Upvotes: 410
Reputation: 11
Had the same problem. I am using _getch()
just before the return statement. It works.
Upvotes: 1
Reputation:
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;
}
Upvotes: 2
Reputation: 782
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...
Upvotes: 19
Reputation: 1614
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 11807
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
Upvotes: 2
Reputation: 365
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;
}
Upvotes: 1
Reputation: 7
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;
}
Upvotes: -1
Reputation: 322
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.
Upvotes: 0
Reputation: 3113
(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.
Upvotes: 0
Reputation: 1089
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.
Upvotes: 4
Reputation: 6260
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 - EDIT: see below./SUBSYSTEM:CONSOLE
to the linker.
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.
Upvotes: 7
Reputation: 1693
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.
Upvotes: 1
Reputation: 9100
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.
Upvotes: 3
Reputation: 7517
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.
Upvotes: 2
Reputation: 100009
Put a breakpoint on the return
line.
You are running it in the debugger, right?
Upvotes: 22
Reputation: 828012
You can use cin.get();
or cin.ignore();
just before your return statement to avoid the console window from closing.
Upvotes: 3
Reputation: 26384
The standard way is cin.get()
before your return statement.
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World";
cin.get();
return 0;
}
Upvotes: 42