忽略一行输入中的某些输入
Ignore certain inputs in a line of inputs
我正在制作一个只接受整数对输入的函数。这将在 while 循环中重复,直到用户输入 "-5 -5"。如果我输入"5 7 -4 3 10 1 -2 0 -5 -5",它将全部作为有效输入。如果输入中有像"e p 5 7 -4 3 10 1 -2 0 -5 -5"这样的字符,我的代码将忽略"e p 5 7 -4 3 10 1 -2 0 -5 -5"的整个输入。相反,我只想跳过第一对"e p",仍然保留其余的。
这个呢,"e p erewr djfe -4 3 ekjea 23 -2 0 -5 -5"?在这种情况下,我只想要" -4 3 -2 0 -5 -5"。我怎样才能做到这一点?谢谢。
这是我的代码:
int input1 = 0;
int input2 = 0;
do {
cin >> input1 >> input2;
if (cin.fail()) { // This will handle non-int cases
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
} else {
setValues(input1, input2);
}
} while((input1 != -5 && input2 != -5));
我提出这个解决方案没有cin.fail((,但使用strings,stoi并尝试捕获块。
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
string input1;
string input2;
int in1,in2;
bool ok=false;
bool ok2=false;
bool ignore=false;
do
{
ok=false;
cin>>input1;
try
{
in1=stoi(input1);
ok=true;
}
catch(...)
{
}
cin>>input2;
ok2=false;
if(ok)
{
try
{
in2=stoi(input2);
ok2=true;
}
catch(...)
{
}
if(ok2)
setValues(in1,in2);
}
}
while((in1 != -5 && in2 != -5));
}
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在C++中,我如何接受不同于同一行的用户输入
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 忽略一行输入中的某些输入
- 丢失读入的每一行输入中的最后一个令牌
- 一行输入到多个变量
- 如何检查从键盘输入的特定整数值是否存在于C++文件中的一行或多行中
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 与 C++ 中一行中的用户输入数组元素相关的问题
- 如何在应用程序进行大量printf时,在固定输入的终端中设置最后一行
- 使用 getline 从输入文件读取一行到输出文件
- 标准输入未读取最后一行
- C :使用getline从文本文件中输入,要么跳过第一行,要么将其余部分弄乱
- 在一行中输入具有其他输入类型的多个单词字符串
- 如何在用户输入值后写入.csv文件的下一行,而不会覆盖以前的值?
- 如何强制 getline() 一次输入一行
- 如何让控制台在输入一行文本时输出某些内容
- 在c++中一次向字符串数组中输入一行
- 程序只在文本文件中输入一行