c++ - 如何在 C++ 上使用 Ctrl + D 结束用户的输入
问题描述
(我在 Windows 上使用 VS Code)
我正在尝试从用户那里获取多行输入,并且当用户点击“Ctrl + D”时输入必须结束,然后它必须继续进入其余代码
#include <iostream>
using namespace std;
int main()
{
string x, input, output, choice;
cin >> x;
cin >> choice:
getline(cin , input) ; // need this part to end when user hits Ctrl + D and i
need more than one line of input
// Rest of my code already completed
}
解决方案
Ctrl D 对应于 ASCII 字符 EOT,其十进制值为 4。因此,
getline(cin, input, '\x04');
这适用于多行,getline 将在您执行 Ctrl D 然后输入后返回,剥离 Ctrl D 以及您在输入之前输入的任何内容。
推荐阅读
- java - java:静态变量不能改变值
- java - How do I make Graphics2D draw a tooltip on Mouse Hover
- css - React Navigation - Creating a tab navigator element is not working?
- android - Xamarin Android Image cropper
- javascript - 缺少样式的附加元素
- node.js - 我如何访问 Apostrophe CMS 上页面的子页面小部件
- node.js - 在 node.js 应用程序上保持打开数据库连接是一种好习惯吗?
- assembly - 尝试打印号码时出错
- reactjs - formik表单验证不起作用reactjs
- c++ - PolyHook x64Detour 未连接到 Direct3DCreate9