very basic C++ program closes after user input for no particular reason?

前端 未结 7 1193

I just started learning C++ and I wrote this sample program from the text and when I compile and run it, it just closes after the user inputs any number and presses enter.

7条回答
  •  心在旅途
    2021-01-18 05:02

    It closes because the execution reaches return 0; and there is nothing left to do.

    If you want the program to wait before closing you could add an something like this:

    cout << "Press enter to exit...";
    cin  >> someVarThatWontBeUsed;
    

    You could also run the program from the command line instead of running the .exe. It will reach end of execution anyway but the prompt will stay open.

提交回复
热议问题