How to use istream_iterator to read input from cin twice?

Discussion in 'C++' started by oliu321@gmail.com, Mar 22, 2007.

  1. Guest

    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
     
    , Mar 22, 2007
    #1
    1. Advertising

  2. David Harmon Guest

    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::streamsize>::max(), '\n');
     
    David Harmon, Mar 22, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 22, 5:31 pm, David Harmon <> wrote:
    > 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::streamsize>::max(), '\n');


    It works great, thanks a lot.
     
    , Mar 23, 2007
    #3
  4. Guest

    On Mar 23, 9:55 am, wrote:
    > On Mar 22, 5:31 pm, David Harmon <> wrote:
    >
    > > 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::streamsize>::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));
     
    , Mar 23, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,108
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,177
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,701
    Alex Vinokur
    Mar 6, 2005
  4. Dave
    Replies:
    1
    Views:
    419
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    May 22, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,685
    Fernando
    Nov 16, 2011
Loading...

Share This Page