What is the Best Practice for Combating the Console Closing Issue?

前端 未结 10 1738
悲&欢浪女
悲&欢浪女 2020-11-27 22:21

After compiling console programs the console window closes immediately after running. What is the best practice for keeping it open? I\'ve searched google loads, I\'m used t

10条回答
  •  悲&欢浪女
    2020-11-27 22:49

    cin is grossly inelegant but easy for the forgetful to derive:

    {
      char c;
      std::cin >> c;
    }
    

    That holds the window open until you type a character /* edit */ and hit enter.

    std::cin.get() will close the window the moment you type a character, which, depending on how easily you become habituated, runs a marginally greater risk of "whoops, I wish I hadn't closed that!" than the two-keystroke operator>>(istream &).

    Both differ from a system("pause") in that they return in a program-accessible way the value of the character you typed, so, if as not infrequently happens, one kludge leads to another, you can write a switch statement based on what you typed to (for example) exit immediately, write some values to a log, run it again, etc.

提交回复
热议问题