如何在 C++ 中检测'Enter Key'?
how to detect 'Enter Key' in c++?
我想检测按下回车键中断循环。如果用户连续按 2 进入,循环中断。我正在使用矢量来存储用户输入。所有变量的类型都是整数。
#include <iostream>
#include <vector>
using namespace std;
int main()
{ int buffer;
vector<int> frag;
do
{
cin >>buffer;
frag.push_back(buffer);
}while(frag.end()!='n');
}
如何从错误消息中转义"与'运算符!='不匹配(操作数类型为'std::vector::iterator....."?
您可以将std::cin.get
与n
进行比较:
std::vector<int> vecInt;
char c;
while(std::cin.peek() != 'n'){
std::cin.get(c);
if(isdigit(c))
vecInt.push_back(c - '0');
}
for (int i(0); i < vecInt.size(); i++)
std::cout << vecInt[i] << ", ";
The input : 25uA1 45p
The output: 2, 5, 1, 4, 5,
如果要读取两个整数值,则在第二次按Enter键后,它将停止读取:
std::vector<int> vecInt; int iVal, nEnterPress = 0; while(nEnterPress != 2){ if(std::cin.peek() == 'n'){ nEnterPress++; std::cin.ignore(1, 'n'); } else{ std::cin >> iVal; vecInt.push_back(iVal); } } for (int i(0); i < vecInt.size(); i++) std::cout << vecInt[i] << ", "; Input: 435 (+ Press enter ) 3976 (+ Press enter) Output: 435, 3976,
相关文章:
- Alt+Enter 在 Win32 应用中,管理大小调整和分辨率
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- std::<key-value>不同类型的对向量
- 修复完成输入后重复"enter names of teacher"?
- .value( "key" , default) 不适用于空的 json 对象吗?
- 在 wxWidgets 中的 wxTextCtrl 上手动触发 ENTER 键按下事件C++?
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- std::set<Key,Compare,Allocator>::find() 函数使用"<"运算符而不是"=="运算符背后的直觉是什么?
- std::move(key) 同时迭代unordered_map<字符串,字符串>?
- cpp / c ++中的grpc客户端代码,元数据x-api-key/x-goog-api-key不起作用,给了我语音A
- 如果我按enter键,我如何退出c++中的循环
- 大多数代码不仅显示欢迎消息和"enter pin"?
- 黄金描述的C++ "Key Function"是什么?
- 如何防止按 Enter 后命令行关闭
- 从更新查询获取'Cannot insert duplicate key'
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- 将"Enter Key"值分配给字符串 [C++]
- 如何在 C++ 中检测'Enter Key'?