error while parsing using stringstream

I want to parse a semicolon seperated string into integers. I used stringstream, it parses the first string but it fails while parsing the second one.
Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
        std::stringstream buf;
        std::stringstream s;
        std::string str;
        s<<"123;456;";
        int i,j =0;
        std::getline(s, str, ';');
        buf.str(str);
        if (buf>>i)
                std::cout<<"i="<<i<<"\n";
        else
                std::cout<<"failed parsing "<<buf.str()<<"\n";
        std::getline(s, str, ';');
        buf.str(str);
        if (buf>>j)
                std::cout<<"j="<<j<<"\n";
        else
                std::cout<<"failed parsing "<<buf.str()<<"\n";
}


Output is:
----------------
i=123
failed parsing 456


What is the problem?
Thanks in advance.
After line 10 ( buf >> i ) buf is empty, thus not in a good state any more.
To reset the stream state to good, you need to call buf.clear() before line 15
You might find my posts in this thread useful:
http://www.cplusplus.com/forum/general/17771/

Good luck!
thank you very much for your answers.
"clearing" solved my problem.
Topic archived. No new replies allowed.