Program ends automatically

Hai
first of all I’d like to thank cplusplus.com members for the information they give to those who want to learn C++ .
__________________________________
I’m using C-Free , when i build a program it works good and " press any key to continue " sentence appears and the program do not terminate till I Press any key.
when I save this program as a .exe for example so I can share it , it also works fine but after main () ends , the program ends immediately before I press any key so I can’t actually see any thing.
what is the reason for this to happen ?
* sorry for bad englsh.
thanks.
Last edited on
Can you show us your code?

You can solve this in many ways. You can use system("pause"); (Although its not good, but if its just for school work and learning and stuff its fine.) You can have it at the end of the program above return 0;

Or you can put cin.get(); instead, which I think is a much better alternative.
Thanks , system ("pause") solved it. :)
closed account (SECMoG1T)
Well probably your are using an IDE, some of them do provide this functionality however it isn't integrated the final EXE, so you'll have to provide your own means of switching the console to stand by mode after execution just as TarikNeaj suggested.
Last edited on
Is there any particular reason something as popular as visual studio 2013 wouldnt provide that functionality, while for example codeblocks does?
Use Ctrl+F5, to run solution, not just F5.
Note that this will run solution without debugger attached (like code::blocks does by default)

If you want to use debugger and keep window open, place a breakpoint to the closing bracket of main() (Works in both C::B and VS)
What would be the difference between running solution with or without debugger? I know what it means to debug your code, but when I just run it via F5, it doesnt debug my code it just runs normally?
but when I just run it via F5, it doesnt debug my code it just runs normally?
It attaches a debugger and gathers data while program runs. If something happens it will show you stack trace, local variables content, etc. Additionally it will stop on breakpoints allowing to see program state at some moment and continue debugging from that place.
So what would be the difference between running ctrlF5 mode, and release mode?
closed account (SECMoG1T)
Yes i believe there is a reason, for those IDEs that provide the functionality it's only meant for testing and doesn't belong to the overall program logic ,bear in mind that there are some "console" programs that are run in the background by other major processes, what would happen if such a process couldn't return control to the OS just because it's stuck somewhere waiting for some "press any key " which it doesn't even understand ;) , bet that would be quite problematic to debug.

srry guys i believe it took sooooo long typing but i guess a got something, hehehe
Last edited on
release and debug mode are about how code is generated, what will be included in executable and so on.

F5 abd Ctrl+F5 is about if program will currently launch with debugger attached or not.

You can run debug build without debugger attached, and you can run release build with debugger attached and in this case you will just lose information about variable names (and some variables existence in general) and function names.
release and debug mode are about how code is generated, what will be included in executable and so on.


Would you mind mentioning some of those included/excluded executables?

Also, thanks for all the info :)
Names debug and release are just referring to two default sets of compiler preferences. You can change those manually too and even create third build target. For exampe one which will work only on Intel i7 CPU, but will be fastest.

Generally release builds are compiled on highest optimisation level. On that code is reordered, meaning it is not executed in same order as written (semantics is preserved, so for user nothing changes) and can even interweave instruction for different lines, inlines functions, devirtualizes function calls, makes as much computations as possible in compile time, etc. That makes it hard to clearly determine which line in source leads to problems in program.
Also it usually strips symbols from resulting file making executable lighter, but at the cost of losing most of the names. Therefore you wont see something like invalid memory access in myclass::calculate() on line 34, it will look like 0x0000005C memory cannot be read. Requested at 0x00FE036A

Thank you :) so as far as I understand. You run it in debug while you're creating the program and testing it, but once its 100% done. and say, you want to sell it to microsoft, would it be used in release mode then?
Well, you may say that. But you can do other things with targets.
For example in project I am currently working on there are four targets in makefile:
legacy: 32 bit application, links everything statically, works even on Windows 2000. Big executable size, slower, good backward compatibility.
modern: 64 bit, links to Visual C++ runtime, requires at least Windows Vista. Faster, smaller, requires modern OS.
debug-32, debug-64: Same as two previous, but without optimisations and with debug symbols included.
Topic archived. No new replies allowed.