2

I have a problem, when I try to open my Hello World.exe file (that I created by following a tutorial). It immediately closes without giving me the chance to read or see if I have done everything correctly.

As you can see, I need help on how to keep it open, without instantly closing.

wovano
  • 4,543
  • 5
  • 22
  • 49

3 Answers3

9

You can either put a break point before the end of main or try the following:

int main()
{
   //...
   std::cin.get();
   return 0;
}

It is going to wait for you to press some key to exit the console.

EDIT: It is better to add break point which do not change existing code.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • 1
    Note that this keeps your program running even if you start it from an existing command window and don't need it. – Sarien May 14 '13 at 17:21
  • 1
    Put a breakpoint at `return 0;`, don't tweak the code to match the debugging environment. – Mooing Duck May 14 '13 at 17:53
2

In console applications there are a couple of things you can do to stop the window from closing on you such as using system("pause") (not so recommended though), getch(), std::cin >> x etc at the end of the application.

Another option is to start a cmd window, cd to the location of the exe and run it like any other console application is meant to be ran, that way it wont just close on you, it'll simply exit.

Edward A
  • 2,291
  • 2
  • 18
  • 31
  • 2
    Tip: Windows 7 has an extended shell context menu item (hold shift and right click) to open a console in that directory. I personally added that to the normal shell context menu as well. – chris May 14 '13 at 16:55
  • Well, I guess I might see that for myself when Blue gets finished. The only thing I've seen that I like is the new copy dialog. No Aero alone outweighs that. – chris May 14 '13 at 17:01
  • WOW! I never would have considered to use system("pause") for such a trivial task. LOL. – Devolus May 14 '13 at 17:03
  • @chris Honestly Windows 8 is not so bad, it has greatly improved usability for multiple monitors, the ui is quite clean, it wakes up from sleep faster than you can blink, etc. The biggest problem that I've encountered is that once every few months win8 is going to go full retard and have a message pop up saying "windows is going to restart in 15 minutes", and from that moment on you can't do anything to it to make it stop. Although, there's a hidden option somewhere to stop it from doing that again, but the first time sucks. – Edward A May 14 '13 at 17:13
  • @EdwardA, Interesting. Might try it with Blue. That wouldn't bother me if there was any way to make it stop, even more advanced than clicking a button. – chris May 14 '13 at 17:24
1

In VS2017, you can specify that the executable is a Console app, not a Windows app. This makes your application run in a "Microsoft Visual Studio Debug Console", with "Press Any Key To Close This Window" appearing at the end of the console output:

Right click the project to bring up Properties.

Linker > System > Subsystem > Select "Console".

Press Apply before closing.

Big Al
  • 980
  • 1
  • 8
  • 20