Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to use istream_iterator to read input from cin twice?

Reply
Thread Tools

How to use istream_iterator to read input from cin twice?

 
 
oliu321@gmail.com
Guest
Posts: n/a
 
      03-22-2007
I have the following code:

vector<int> A, 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

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      03-22-2007
On 22 Mar 2007 13:49:48 -0700 in comp.lang.c++, 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');

 
Reply With Quote
 
 
 
 
oliu321@gmail.com
Guest
Posts: n/a
 
      03-23-2007
On Mar 22, 5:31 pm, David Harmon <sou...@netcom.com> wrote:
> 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.

 
Reply With Quote
 
oliu321@gmail.com
Guest
Posts: n/a
 
      03-23-2007
On Mar 23, 9:55 am, oliu...@gmail.com wrote:
> On Mar 22, 5:31 pm, David Harmon <sou...@netcom.com> wrote:
>
> > 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<int> A, 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));

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Using cin.get() and cin.eof() ends up in an endless loop Fernando C++ 4 11-16-2011 07:19 PM
cin and cin.getline() Aleander C++ 5 03-06-2005 03:57 PM
std::cin.ignore() and std::cin.clear() Chris Mantoulidis C++ 5 01-06-2004 11:08 PM
cin and cin.clear() problem TaiwanNoWhere C++ 8 10-17-2003 05:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57