【问题标题】:Using code from standard library to stop output from exiting in Turbo C++在 Turbo C++ 中使用标准库中的代码阻止输出退出
【发布时间】:2013-11-08 08:56:50
【问题描述】:

这里的许多人告诉我停止使用clrscr()getch() 等,我已经开始使用标准库学习 C++,现在我想遵循标准库如何停止立即输出运行后退出?

include <iostream.h>
include <conio.h> // Instead of using this

    void main(){
        cout << "Hello World!" << endl;
        getch(); // Instead of using this
        }

【问题讨论】:

    标签: c++ c standard-library turbo-c++


    【解决方案1】:

    您可以直接从命令行运行二进制文件。在这种情况下,程序执行完成后,输出仍然会在终端中,您可以看到它。

    否则,如果您使用的 IDE 会在执行完成后立即关闭终端,您可以使用任何阻塞操作。最简单的是scanf (" %c", &amp;dummy);cin &gt;&gt; dummy; 甚至getchar (); 以及Adriano 的建议。虽然您需要按 Enter 键,因为这些是缓冲输入操作。

    【讨论】:

      【解决方案2】:

      只需将getch() 替换为cin.get(),如下所示:

      include <iostream>
      
      using namespace std;
      
      void main()
      {
          cout << "Hello World!" << endl;
          cin.get();
      }
      

      更多详情请见get() function documentation。仅供参考,您可以这样做,例如,等到用户按下特定字符:

      void main()
      {
          cout << "Hello World!" << endl;
          cout << "Press Q to quit." << endl;
          cin.ignore(numeric_limits<streamsize>::max(), 'Q');
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 2010-09-11
        • 1970-01-01
        • 2012-02-12
        • 2011-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多