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

前端 未结 7 1260

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 04:39

    The program closes because there's nothing more for the program to do. It outputs the final statements really really fast and then reaches return 0 which causes it to exit. You'll want to do something there to pause the program.

    On Windows, a basic way to do that is system("pause"); (you will need #include )

    cin.getline is a more standard way to do it.

提交回复
热议问题