I'm trying to repeatedly read integers, and for tokens that are not integers, to read and handle them as strings. I thought the following would work:
string s;
int x;
while(1) {
    if (cin >> x)                                     // try reading as int
        cout << "read int: " << x << endl;
    else {                                            // try reading as string
        cin.clear();
        if (cin >> s) 
            cout << "read string: " << s << endl;
        else
            break;                                    // eof
    }
}
However, for input (with newline at end)
1 2 a
it gives output
read int: 1
read int: 2
It seems that after clearing the state of the stream is not exactly the same as before, and in fact cin >> s fails. Why is this, and how can I achieve my desired behaviour? 
To clarify, I can't parse the string as an int because I have not "learned" that yet.
 
     
     
    