我正在尝试将 stringstream 与 peek() 和 get(n) 一起使用。它仅适用于第一个值 Name,然后为除 weight 之外的所有内容提供空值,重量为 0(使用 cout 打印时)。
stringstream ss;
ss.str("John Smith, Toyota, 160, brown blue, orange");
//Extract into these variables:
string Name = "" ;
string car = "" ;
int weight = 0;
string hairColor = "";
string eyeColor = "";
string favoriteColor = "";
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
Name += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
car += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
weight += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
hairColor += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
eyeColor += temp;
}
while (ss.peek () != ',')
{
char temp;
ss. get(temp);
favoriteColor += temp;
}
cout << "Name is: " << Name << endl;
cout << "car is: " << car << endl;
cout << "weight is: " << weight << endl;
cout << "Hair color is: " << hairColor << endl;
cout << "Eye color is: " << eyeColor << endl;
cout << "Favorite color is: " << favoriteColor << endl;
这里有什么问题?