I'm create c++ empty project in visual studio 2010. Write simple program (e.g. hello world) and run it. But console window immediately closed! What to do?
-
wait for input, say `std::cin >> somevar;` – sehe May 02 '11 at 08:29
-
it's a bad decision. Maybe somewhere in the settings of the project should indicate something...but I do not know what and where – Witcher May 02 '11 at 08:32
-
you mean you don't like to wait? Then just start a console application as a... console application perhaps. In my experience IDEs frequently do the wait for you when not debugging. Don't remember about VS really – sehe May 02 '11 at 08:36
-
http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately/2529803#2529803 – nabulke May 02 '11 at 08:37
4 Answers
It "disappears" because your program runs to an end and you don't tell the console to "stay".
There are at least two ways to solve this problem.
Press keys ctrl + F5 to start your program if you don't wanna debug your program. It will hold the console window for you until you press any key.
If you wanna debug your program with F5, you can explicitly "hold" the console as:
// trivial, just to hold the console for you
std::cin.get();

- 14,327
- 7
- 45
- 69
You most likely don't have anything that would temporarily stop the program from going any further. Such as waiting for some simple input. You could add that. A "press any key to continue" if you will. Or just launch your application from within a cmd window. It will still immediately terminate, but at least the window will stay opened.

- 19,692
- 7
- 68
- 77
If you run a console program under the debugger (for example by pressing F5) the program will not stop unless it hits a breakpoint.
If you run the console program not using the debugger (such as with Ctrl-F5), it will stop when the program ends and display a "Press any key to continue . . ." prompt.
One easy workaround is to set a breakpoint at the end of main()
or wherever else might be appropriate.

- 333,147
- 50
- 533
- 760
You can set a breakpoint on the last line of main
. Go to the last }
and press F9 to do that.
int main()
{
return 0;
} // <--- Set a breakpoint on this line (Press F9)

- 15,059
- 3
- 48
- 64