使用条件确定字符串中的元素
Use conditional to determine elements within a string
这是对我上一个问题 (Last item of istringstream repeats?) 的扩展。
我需要确定一个字符串是否包含两条数据,还是三条数据。我正在使用 'if' 条件来确定这一点,但是我没有得到我希望的结果 - 我意识到我遗漏了一些东西。
if 的第一部分检查是否只有名字和年龄 - 如果这不起作用,则应尝试下一部分(my else if),它允许三个 - 其中有第一个、最后一个和年龄。当我尝试 first、last 和 age 时,这是行不通的(尽管如果我只有 first 和 age 就可以),并且我从底部得到结果 else声明 - 这意味着其他两个失败了。这可以向我解释,并可能帮助我修复我的代码,以便它完成我想要的吗?非常感谢您的帮助!
我的代码是:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string first;
string last;
string fullName;
int age;
//string number;
string inputText("Jane Smith 18");
istringstream iss(inputText);
if (iss >> first >> age)
{
cout << "Only first and age detected.";
}
else if (iss >> first >> last >> age)
{
while (iss >> first >> last >> age)
{
iss >> first >> last >> age;
fullName = first + " " + last;
cout << fullName << " " << age << endl;
}
}
else
{
cout << "Failed";
}
system("pause");
return 0;
}
问题
下一行的条件
if (iss >> first >> age)
计算为 false
,因为无法从字符串中提取 age
。
下一行的条件
else if (iss >> first >> last >> age)
不会从 iss
中提取任何输入,因为 iss
已经处于错误状态。条件计算结果为 false
.
也就是说,语句
下的代码块
else
被执行。
一个解决方案
// Extract the first name
if ( !(iss >> first) )
{
// If we fail to extract the first name, the string is not right.
// Deal with error.
cout << "Failed" << endl;
exit(0); // ???
}
// Try to extract age.
if ( iss >> age )
{
// Age was successfully extracted.
cout << "Only first and age detected.";
}
else
{
// Clear the error state of the stream.
iss.clear();
// Now try to extract the last name and age.
if ( iss >> last >> age )
{
fullName = first + " " + last;
cout << fullName << " " << age << endl;
}
else
{
// Deal with error.
cout << "Failed" << endl;
exit(0); // ???
}
}
这是对我上一个问题 (Last item of istringstream repeats?) 的扩展。
我需要确定一个字符串是否包含两条数据,还是三条数据。我正在使用 'if' 条件来确定这一点,但是我没有得到我希望的结果 - 我意识到我遗漏了一些东西。
if 的第一部分检查是否只有名字和年龄 - 如果这不起作用,则应尝试下一部分(my else if),它允许三个 - 其中有第一个、最后一个和年龄。当我尝试 first、last 和 age 时,这是行不通的(尽管如果我只有 first 和 age 就可以),并且我从底部得到结果 else声明 - 这意味着其他两个失败了。这可以向我解释,并可能帮助我修复我的代码,以便它完成我想要的吗?非常感谢您的帮助!
我的代码是:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string first;
string last;
string fullName;
int age;
//string number;
string inputText("Jane Smith 18");
istringstream iss(inputText);
if (iss >> first >> age)
{
cout << "Only first and age detected.";
}
else if (iss >> first >> last >> age)
{
while (iss >> first >> last >> age)
{
iss >> first >> last >> age;
fullName = first + " " + last;
cout << fullName << " " << age << endl;
}
}
else
{
cout << "Failed";
}
system("pause");
return 0;
}
问题
下一行的条件
if (iss >> first >> age)
计算为 false
,因为无法从字符串中提取 age
。
下一行的条件
else if (iss >> first >> last >> age)
不会从 iss
中提取任何输入,因为 iss
已经处于错误状态。条件计算结果为 false
.
也就是说,语句
下的代码块else
被执行。
一个解决方案
// Extract the first name
if ( !(iss >> first) )
{
// If we fail to extract the first name, the string is not right.
// Deal with error.
cout << "Failed" << endl;
exit(0); // ???
}
// Try to extract age.
if ( iss >> age )
{
// Age was successfully extracted.
cout << "Only first and age detected.";
}
else
{
// Clear the error state of the stream.
iss.clear();
// Now try to extract the last name and age.
if ( iss >> last >> age )
{
fullName = first + " " + last;
cout << fullName << " " << age << endl;
}
else
{
// Deal with error.
cout << "Failed" << endl;
exit(0); // ???
}
}