I have created a Win32 Console Application in Visual Studio but when I start the program the console apears just for a second and then disapears again. What should I do that the console remains on the screen ?
Asked
Active
Viewed 197 times
1
-
possible duplicate of [How to keep the console window open in visual c++?](http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c) – Bo Persson Mar 11 '12 at 06:12
-
Use the search button please. This question has been asked upwards of a million times – Marlon Mar 11 '12 at 07:42
3 Answers
3
Well, the program has finished running, so it closes.
Either make the program wait for input (e.g. with getchar()
), or press Ctrl-F5 to run the program without debugging (but then you won't be able to set breakpoints and stuff).

user541686
- 205,094
- 128
- 528
- 886
-
@MichaelBurr: I was going to mention that, but for a beginner it might be confusing because the window suddenly goes away into the background. :) – user541686 Mar 11 '12 at 06:15
-
1The problem with a breakpoint is that the IDE will automatically pop up to the front, obscuring the console window. Yes, it will work, but it adds an extra step. – Cody Gray - on strike Mar 11 '12 at 06:15
-
All true, but I suppose it depends on what you want to do - if you're debugging you either set a breakpoint and go through the extra step of bringing the window back in front of the debugger, or you take the other step of adding a `getchar()` that you really don't want in the program (so don't forget to take it back out). In all honesty, I don't know why F5 doesn't behave the same as Ctrl-F5 as far as this bit of behavior goes. I know that I would rather it did. – Michael Burr Mar 11 '12 at 06:20
-
I use conditional compilation to insert a `getchar()` statement or equivalent. Something like `#ifdef DEBUG`, just to be sure that I don't forget to take it out. – Cody Gray - on strike Mar 11 '12 at 06:32
0
You can set breakpoints anywhere in your code to make it stop. If you just want to see the output of the program when it's done, try setting a breakpoint on the last line of main().

aldo
- 2,927
- 21
- 36
0
This is happening because the program has nothing to wait for before exiting.
Try running std::cin.get();
before main()
returns to make the console wait for keyboard input.

Alex Z
- 2,500
- 2
- 19
- 23