0

I am a beginner in an intro course and I use pocket C++ for my compiler. Whenever I start a new assignment two files save. A .cpp for the code and a .exe to run the app. Whenever I run the exe, it closes immediately after the final input or action is over. Why does this happen ? Any help would be great!

ernie
  • 37
  • 1
  • 8
  • 4
    Run it through the command prompt. You'll need to lookup how to do this if you don't know. – Carcigenicate Feb 24 '16 at 04:36
  • 2
    I think this is normal behaviour to close the program when it done all the work. – Dmitriy Zapevalov Feb 24 '16 at 04:39
  • which compiler/IDE are you using? On MSVC you can press ctrl+F5 (run without debug) to make it stop without exiting – phuclv Feb 24 '16 at 04:44
  • This is normal. What makes you think otherwise? – user207421 Feb 24 '16 at 04:45
  • 2
    what would you want the program to do after the final action? – 2785528 Feb 24 '16 at 04:54
  • I know this is an old question and I see several others like it without what I'm going to say here. I think context would help. For a while I wondered the same thing and it only dawned on me after seeing a simple console application with a menu in a text book. The last option in the menu was "Exit Program". The last action of the program was to close, thus "Return 0;" in the main was the line of code that was executed. When there isn't a pause of some kind while programming such things, you don't actually see what your code does because the console closes so quickly. – Dan Aug 21 '19 at 19:12

3 Answers3

3

This seems like you need to learn very basics of C++ programming.

  • You can put a breakpoint at the enclosing brace of main() if your debugger support breakpoints.
  • cin.get(); : This is normally preferred.
  • getch(); : This should work, but some compilers might not work with this. And also this is not in C/C++ standard library
  • system("PAUSE"); - is also worked on this and it is not a good way to do this since it relaunch the shell in another process. It is unnecessary.

But with this IDE you have to keep aligh your program as follows. Since you are using Pocket C++ as your IDE you have to follow up following things before you start compiling:

  • .cpp must contain a main() function (self contained)
  • cpp file will compiled through C++ 14 standard
  • can be only use static linking (which means your program should not depend on external .dll Eg: C++ runtimes)

Hopefully in next releases they will add more support to this tiny IDE. (like several cpp files and linking external libraries).

Shamendra
  • 176
  • 3
  • 14
0

C++ program closes as soon as last statement get executed. One of the trick used in C/C++ program to remain open at end is use getch(); statement.

It's a character input function which wait for user to press any key. So basically it work like "Press any key to exit".

0

Basically the .exe didn't encounter any statements that caused the program to stop and wait for something, like user input.

Unlike some C++ editors will close once a value is returned from main(). Some C++ editors will halt this process and output some information about the program like how long the program took to execute or what value was returned to main (zero for a successful program execution or some value corresponding to the type of error the program encountered).