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

前端 未结 7 1187

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:05

    Imagine you were designing a model for application execution. You have two choices:

    A) When the end of a program is reached it shall terminate.

    B) When the end of a program is reached the program shall remain alive in some strange limbo state. It will still retain system resources and will not actually be doing anything, but in order to close the user must terminate it explicitly.

    I think anyone would go for option A here, and that is what you are seeing. The end of main is reached and your program exits.

    If you would like it to pause at the end take some input from the user, i.e.,

    char c;
    std::cin >> c;
    return 0;
    

提交回复
热议问题