By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

How to use istream_iterator to read input from cin twice?

P: n/a
I have the following code:

vector<intA, B;
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(A));
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(B));

If I input the following

1 2 3 4 a <CR>

then I can get A as 1,2,3,4 which is OK, but then the second copy
won't read anything and the B becomes empty.

I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B. I guess somewhat you should tweak the cin to force
the second copy work. But how?

Thanks
Ou

Mar 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, ol*****@gmail.com wrote,
>I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B.
Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');

Mar 22 '07 #2

P: n/a
On Mar 22, 5:31 pm, David Harmon <sou...@netcom.comwrote:
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, oliu...@gmail.com wrote,
I can see that cin has bad bit set to true after the first copy which
it should be. But even after I called a cin.clear() before the second
copy, I still couldn't get the program to try to read my second line
of input into B.

Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
It works great, thanks a lot.

Mar 23 '07 #3

P: n/a
On Mar 23, 9:55 am, oliu...@gmail.com wrote:
On Mar 22, 5:31 pm, David Harmon <sou...@netcom.comwrote:
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, oliu...@gmail.com wrote,
>I can see that cin has bad bit set to true after the first copy which
>it should be. But even after I called a cin.clear() before the second
>copy, I still couldn't get the program to try to read my second line
>of input into B.
Even after you call cin.clear(), the funky character is still waiting to
be read in the input stream. You need to do something to read it. My
favorite would probably be
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');

It works great, thanks a lot.
By the way, for those who are interested in this post, here is a
working code:

vector<intA, B;
// Type 1 2 3 4 a <CR>
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(A));
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Type 4 5 6 7 b <CR>
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter(B));

Mar 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.