+ 1

return 0 not working in exe

Im using the regular g++ to compile my code. I'm just wondering cause with g++ and even codeblocks, return 0 is working fine. except when I double click the exact .exe file that i compiled. Where it will just closed right after I enter an input on the code. I have a simple input program in here where the user can input a number to set age. it works fine with the g++ cmd console and codeblocks. but not when I double click the exe file.

27th Mar 2017, 11:25 AM
Evil Minion
Evil Minion - avatar
4 Antworten
+ 1
Thanks for the link. yah all of those, I've already done. it worrks fine with g++ and other IDE. but my problem is when Im running the .exe file that is sitting on the folder im working on. well as im wondering maybe this is not really going to work with a straight linear program like hello world, for most of the programs are running loop, where in one function is waiting an input for you to exit the application. correct me if im wrong. Thats just what i think happening ;)
28th Mar 2017, 1:25 AM
Evil Minion
Evil Minion - avatar
0
The purpose of return 0 at the end of main() is to verify that the program executed without errors. If a non-zero value is returned by main() then the program is flawed. When debugging, Codeblocks waits for the user to enter a key before terminating the program. Notice that this is not related to return 0. When running the app from the cmd, the program will immediately terminate when return 0 is reached in main(). If you are sing Windows, you could add System("pause"); just before return 0. Though a more portable solution might be to add getchar(); just before return 0.
27th Mar 2017, 12:53 PM
NNNicomedes
NNNicomedes - avatar
0
@NNNicomedes thanks for answering. I already did try using systempause and get char. but i think this 2 line off code only works with visual studio. whever i compile it with just g++ it will give me an error saying system was not declared.
27th Mar 2017, 2:19 PM
Evil Minion
Evil Minion - avatar