使用标准库中的代码来停止在Turbo C ++中退出输出

时间:2013-11-08 08:56:50

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

这里有很多人告诉我要停止使用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 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

只需将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');
}