0

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

Elliot
  • 97
  • 1
  • 8
  • I just tried changing the 'int' variable to a 'string', and the 'age' variable went in without doubling. I'm guessing, but is 'sstream' not viewing placing the data into the 'int', as finishing the whole string? If so (or something similar), how would I get this to work, so that 'int' would be stored separately? – Elliot Oct 25 '15 at 02:30
  • remove while loop, no need here – MORTAL Oct 25 '15 at 02:30
  • 2
    You need `while (iss >> first >> last >> age)` – Neil Kirk Oct 25 '15 at 02:32
  • @NeilKirk: Hmm. why does the last extraction into `age` not result in eof being set? I understand it for getline, but .... the stream would literally have attempted and failed [due to EOF] to extract another digit character by the time the loop restarts. – Lightness Races in Orbit Oct 25 '15 at 02:34
  • Guys that "duplicate" isn't even for the same programming language. – Lightness Races in Orbit Oct 25 '15 at 02:35
  • 1
    If you'd initialised your variables, you'd see that nothing is "repeated". And if you'd fired up your debugger, you'd have been able to count the number of times the loop runs, examined the stream state at each step, then used that information to determine _why_. – Lightness Races in Orbit Oct 25 '15 at 02:36
  • Ok my link was wrong. – Neil Kirk Oct 25 '15 at 02:38
  • @NathanOliver: What null terminator?? – Lightness Races in Orbit Oct 25 '15 at 02:38
  • 2
    Except: http://ideone.com/jSrIh0 – Neil Kirk Oct 25 '15 at 02:39
  • @NeilKirk I was incorrect. the real reason is `operator bool` on the stream will return `true` if only the EOF flag is true. see the table at the bottom of: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool – NathanOliver Oct 25 '15 at 02:42
  • @LightnessRacesinOrbit - sorry, I'm still learning, but does this mean that 'iss' within the 'while' is basically just checking 'eof'? So continue until 'eof'? I thought it meant do until all items have been split - but that makes a lot more sense. – Elliot Oct 25 '15 at 02:45
  • @Robert_C: Neither the while loop nor the booleanisation of the expression `iss` have any idea that your program is intended to "split all items". It checks the "state" of the stream, which turns bad when an extraction fails (due to e.g. a `'a'` in the wrong place in the buffer which cannot be extracted into an `int`) or when an extraction has been attempted past end-of-file (the "past" here being important). – Lightness Races in Orbit Oct 25 '15 at 14:58

0 Answers0