explain me why this happens |
Because when your program finishes, it gives control to OS. As you did not start command line session directly, your program was running in "temporary" one, which is destroyed as soon as it is not needed anymore.
All IDE worth using knows how (or there is a setting to enable it) to not terminate command line session immideately (they usually run a special runner process which keeps window open after program exits).
This subprogram is disabled when program is run under debugger, as you do not want for anything to interfere with debugging.
You should not interfere with this process: this is the behhavior of all proper console applications.
* If you do not need debugger right now, run without debugger. (This should be default setting, do not know why VS authors decided otherwise)
* If you need debugger, you have problems you need to solve and do not really need to keep window open. In extreme cases, place a breakpoint on return statement in main().
* If you want to run your program outside IDE, do it from command line. Console applications should not be launched by double-clicking on them!