使用标准库中的代码阻止输出在 Turbo C++ 中退出
Using code from standard library to stop output from exiting in 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
}
您可以直接从命令行运行二进制文件。在这种情况下,程序执行完成后,输出仍将在终端中,您可以看到它。
否则,如果您使用的是在执行完成后立即关闭终端的 IDE,则可以使用任何阻塞操作。最简单的是scanf (" %c", &dummy);
或cin >> dummy;
甚至getchar ();
以及阿德里亚诺的建议。虽然你需要按 输入 键,因为这些是缓冲输入操作。
只需将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');
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Turbo我试图制作C TIC TAC TOE游戏,但没有获得正确的输出
- 使用标准库中的代码阻止输出在 Turbo C++ 中退出
- 从turbo c++中的二进制文件读取后出现意外输出