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

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

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

解决方案 2

只是更换 getch()cin.get() 像这样:

include <iostream>

using namespace std;

void main()
{
    cout << "Hello World!" << endl;
    cin.get();
}

有关更多详细信息,请参见 get()功能文档. 。例如,您可以这样做,例如,等待用户按下特定字符:

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

其他提示

您可以直接从命令行运行二进制文件。在该程序完成后执行输出后,您可以在终端中看到它,您可以看到它。

否则,如果您使用的是在执行完成后立即关闭终端的IDE,则可以使用任何阻止操作。最简单的是 scanf (" %c", &dummy); 或者 cin >> dummy; 甚至 getchar (); 以及阿德里亚诺(Adriano)的建议。尽管您需要按Enter键,但由于这些是缓冲输入操作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top