忽略用户输入C++中的字母
Ignoring letters in user input C++
我的代码的简化版本:
vector<double> iV;
double i;
cout << "Enter numbers:n";
while(cin >> i) {
iV.push_back(i);
}
for (auto e : iV) {
if (!iV.empty())
cout << e << endl;
}
它现在所做的是从cin中读取double类型的数字,将它们加载到向量中,然后打印出来。但是,用户必须输入一封信才能提交输入我不想要这个我希望忽略用户输入的任何字母。
例如,
输入数字:
56 f 45.6 200.1 6g
应具有输出:
56
45.6
200.1
6
string process( const string& input ) // Removes all characters except <space>, '.' and digits
{
string ret;
for ( const auto& c : input )
{
if ( c == ' ' || c == '.' || ( c >= '0' && c <= '9' ) )
{
ret += c;
}
}
return ret;
}
int main()
{
string line;
vector<double> iV;
double i;
while ( getline( cin, line ) )
{
line = process( line );
stringstream ss( line );
while ( ss >> i )
{
iV.push_back( i );
}
}
for ( auto e : iV )
{
if ( !iV.empty() )
{
cout << e << endl;
}
}
}
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 当用户超过按钮点击限制时报告
- Visual C++(VS2017)中用户定义的转换不明确
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- RegGetValue在当前用户下找不到名称
- 函数计算用户按下按钮的频率