在C ++程序中暂停控制台 [英] Pause Console in C++ program

查看:173
本文介绍了在C ++程序中暂停控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些是在C ++程序中暂停控制台的最佳方式?

Which is best way to pause the console in C++ programs?


  1. 使用 cin.get / code>

  2. 或使用系统(暂停)

  3. 使用C函数,如 getch() getchar()

  1. using cin.get()
  2. or using system("pause")
  3. or using C functions like getch() or getchar()?

是否使用 system(pause)会导致非可移植代码,并且无法在UNIX中使用

Is it true that use of system("pause") leads to non portable code and can't work in UNIX?

是否使用cin.get()更好地暂停控制台?

Is cin.get() is better to use to pause console?

推荐答案

可能有一个最好的方法(如使用便携式 cin.get c $ c>),良好方式不存在

There might be a best way (like using the portable cin.get()), but a good way doesn't exist. A program that has done its job should quit and give its resources back to the computer.

是的,任何使用 system()

And yes, any usage of system() leads to unportable code, as the parameter is passed to the shell that owns your process.

在源代码中暂停代码迟早会导致麻烦:

Having pausing-code in your source code sooner or later causes hassles:


  • 有人忘记在签入之前删除暂停代码



    • 现在,所有工作伙伴都不知道为什么应用程序不再关闭了。

    • 版本历史记录已损坏

    而是探索您的IDE。它可能有一个选项不运行后关闭控制台窗口。如果不是,这是一个伟大的理由,作为一个开发商值得她/他的钱总是有一个控制台窗口打开附近。

    Instead, explore your IDE. It probably has an option not to close the console window after running. If not, it's a great justification to you as a developer worth her/his money to always have a console window open nearby.

    或者,你可以使这个程序选项,但我个人从未见过一个带有 - keep-alive-when-dead 选项的程序。

    Alternatively, you can make this a program option, but I personally have never seen a program with an option --keep-alive-when-dead.

    故事的道德:这是用户的问题,而不是程序的问题。不要污染您的代码。

    Moral of the story: This is the user's problem, and not the program's problem. Don't taint your code.

    这篇关于在C ++程序中暂停控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆