Last item of istringstream repeats?

空扰寡人 提交于 2019-12-11 19:43:11

问题


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

来源:https://stackoverflow.com/questions/33325548/last-item-of-istringstream-repeats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!