I'm having trouble with splitting a string with sstream. It appears when the loop of cout happens, the last item of data, an integer in my case, is repeated? Is my loop set up wrong??
This is my code:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string inputText("Jane Smith 18");
istringstream iss(inputText);
while (iss)
{
string first;
string last;
int age;
iss >> first >> last >> age;
cout << first << " " << last <<" "<< age << endl;
}
system("pause");
return 0;
}
See the picture - showing how the 'age' variable seems to be repeated. What am I doing wrong?
Thanks for your help in advance!
Console-Example
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…