'In C++ How can I read in different types of data in one instream of the same cin object

I wrote a custom class CMyistream_iterator. Its constructor has an argument of an istream object.

template <class T>
class CMyistream_iterator
{

private:
    T valArray[100];
    int pos;
public:
    CMyistream_iterator(istream& in):pos(0){
        T n;
        int i=0;
        while(in>>n){
            *(valArray+i) = n;
            ++i;
        }      
    }
    T operator*();
};

But I meet some problems when I enter the data.

 int main(){
       CMyistream_iterator<int> inputInt(cin);
       //...somecode...           
       CMyistream_iterator<string> inputStr(cin);
       //...somecode...
    }

Data is entered like this:

22 33 44 tom jack+'\n'

The second class can't be constructed as it can't read in the next two strings.

It seems that when the integer iterator comes to the character 't', it was converted into EOF.

How can I solve this problem?

c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source