Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c++ - Last item of istringstream repeats?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...