替代系统(“ PAUSE”)? [英] Alternative to system("PAUSE")?

查看:59
本文介绍了替代系统(“ PAUSE”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了有关不良 系统( PAUSE)的信息。我尝试了 cin.get(),但是我不知道如何使用它来暂停程序。其他帖子说在声明后放置一个断点。但是我不知道该怎么做。关于如何暂停程序的任何建议?

I've recently read about how BAD system("PAUSE") is. I tried cin.get() but I don't know how to use it to pause a program. Other posts say to put a breakpoint after the statement. But I have no knowledge on how to do that. Any suggestions on how to pause my program? An example would be really appreciated.

推荐答案

假设您确实在Windows上工作,关于 system( PAUSE)是它背叛了对操作系统体系结构的根本误解。您不需要 system( PAUSE)的代码替换,因为代码是解决已知问题的错误位置

Assuming that you are indeed working on Windows, the worst thing about system("PAUSE") is that it betrays a fundamental misunderstanding of your operating system's architecture. You do not need a code replacement for system("PAUSE"), because the code is the wrong place to solve the perceived problem.

初学者喜欢放置 system( PAUSE)或什至是 std这样的便携式替代品: :cin.get()放在程序末尾,因为否则窗口消失 一旦程序结束。但是,这种逻辑存在严重缺陷。您可能会在程序运行时看到的窗口,它使您提出此问题,这不是 程序本身的一部分,而是程序运行环境的一部分

Beginners like to put system("PAUSE") or even a portable alternative like std::cin.get() at the end of a program because otherwise "the window disappears" as soon as the program ends. Such logic, however, is deeply flawed. The window which you probably see while the program runs and which has made you ask this question is not part of the program itself but part of the environment in which the program runs.

但是,典型的控制台程序不得假定其执行环境的详细信息。相反,在通过 std :: cout std :: cin 。谁说您的程序甚至对人类用户可见?您可以读取或写入文件;您可以使用管道;您可以将文本发送到网络套接字。您不知道。

A typical console program, however, must not assume details about the environment in which it is executed. You must instead learn to think in more abstract terms when it comes to input and output via std::cout and std::cin. Who says that your program is even visible for a human user? You may read from or write into a file; you may use pipes; you may send text to a network socket. You don't know.

#include <iostream>

int main()
{
    std::cout << "Hello world\n"; // writes to screen, file, network socket...
}

打开图形窗口并在屏幕上显示文本输出不在您程序的范围之内,但是使用 system( PAUSE)完全假定一个用例并破坏了所有其他用例。

Opening a graphical window and displaying text output on the screen is not in the scope of your program, yet using system("PAUSE") assumes exactly that one single use case and breaks all others.

如果您使用的是Visual Studio之类的IDE,并为按F5键最终导致窗口消失而感到烦恼,那么您有机会看到所有输出,请在此处与操纵程序本身相比,还有三种更明智的选择:

If you use an IDE like Visual Studio and are annoyed by the fact that pressing F5 eventually results in the window disappearing before you have had the chance to see all output, here are three more sensible alternatives than manipulating the program itself:


  • 神秘化。观察到Visual Studio的真正作用是在后台调用Visual C ++编译器以创建* .exe文件。使用 cmd Tools> Visual Studio Command Prompt 打开自己的控制台窗口,找到该* .exe文件的目录并在其中运行(您最终还应该学会在没有Visual Studio帮助的情况下启动编译器,因为这将使您对C ++生成过程有更深入的了解。

  • 按CTRL + F5。

  • 在代码末尾放置一个断点。如果您不知道怎么做,请阅读文档

  • Demystification. Observe that what Visual Studio really does is invoking the Visual C++ compiler behind the scenes in order to create an *.exe file. Open your own console window with cmd or with Tools > Visual Studio Command Prompt, locate the directory of that *.exe file and run it there (you should eventually also learn to start the compiler without Visual Studio's help, because that will give you a deeper understanding of the C++ build process).
  • Press CTRL+F5.
  • Place a breakpoint at the end of your code. Read the documentation if you don't know how.

这篇关于替代系统(“ PAUSE”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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