cin.clear() does not work

Discussion in 'C++' started by AD, Mar 25, 2006.

  1. AD

    AD Guest

    Hi

    I am trying to read elements of a vector from console (cin) in two
    steps ie, read a list of numbers from cin. Sort these. Then read from
    cin again and append in same vector.

    /*****************************************************************/
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<iterator>

    using namespace std;

    int main()
    {

    ios_base::iostate ios = cin.exceptions();
    cin.exceptions(ios_base::eofbit|ios_base::failbit|ios_base::badbit);

    vector<int> v1;

    try
    {
    cout<<"Enter first List:";

    //read first time
    copy(istream_iterator<int>(cin), istream_iterator<int>(),
    back_inserter(v1));
    //I enter some integers followed by either some non-integer or
    ctrl-D & Enter to
    //signify end of stream
    }
    catch(ios_base::failure)
    {
    //always getting a badbit exception even if I enter Ctrl-D
    //which I thought signified eof

    ios_base::iostate s = cin.exceptions();

    if(s&ios_base::badbit) cout<<"\n BadBit Exception";
    else if(s&ios_base::failbit) cout<<"\n Fail Bit Exception";
    else if(s&ios_base::eofbit) cout<<"\n EOF Bit Exception";
    }

    try
    {
    //clear cin state
    cin.clear(); //cin.clear(ios_base::badbit) causes BadBit exception

    //sort input array
    sort(v1.begin(), v1.end());

    cout<<"\nEnter Second List:";

    //read second time
    copy(istream_iterator<int>(cin), istream_iterator<int>(),
    back_inserter(v1));
    }
    catch(ios_base::failure)
    {
    ios_base::iostate s = cin.exceptions();
    if(s&ios_base::badbit) cout<<"\n BadBit Exception";
    else if(s&ios_base::failbit) cout<<"\n Fail Bit Exception";
    else if(s&ios_base::eofbit) cout<<"\n EOF Bit Exception";
    }

    return 0;
    }//endmain()
    /*****************************************************************/

    First Time I enter some integers and then a character or ENTER & Ctrl-D
    (which should work as EOF). I get a badbit exception (surprisingly even
    in case of Ctrl-D I am getting bad bit and not eof) which I clear by
    invoking cin.clear(). Now when I try to input elements from cin again
    (second time) , I get badbit exception immediately (without taking any
    input)..:-(

    Seemingly either cin.clear() is not resetting bad bit or bad bit has to
    be handled differently.
    I even tried cin.clear(ios_base::badbit) but that causes an exception
    at that point itself.

    How can I read cin further once it gets corrupted due to bad input?

    I am compiling it on Windows with g++ compiler.

    thanks & regds
    AD
    AD, Mar 25, 2006
    #1
    1. Advertising

  2. AD

    Daniel T. Guest

    In article <>,
    "AD" <> wrote:

    > Hi
    >
    > I am trying to read elements of a vector from console (cin) in two
    > steps ie, read a list of numbers from cin. Sort these. Then read from
    > cin again and append in same vector.
    >
    > /*****************************************************************/
    > #include<iostream>
    > #include<algorithm>
    > #include<vector>
    > #include<iterator>
    >
    > using namespace std;
    >
    > int main()
    > {
    >
    > ios_base::iostate ios = cin.exceptions();
    > cin.exceptions(ios_base::eofbit|ios_base::failbit|ios_base::badbit);
    >
    > vector<int> v1;
    >
    > try
    > {
    > cout<<"Enter first List:";
    >
    > //read first time
    > copy(istream_iterator<int>(cin), istream_iterator<int>(),
    > back_inserter(v1));
    > //I enter some integers followed by either some non-integer or
    > ctrl-D & Enter to
    > //signify end of stream
    > }
    > catch(ios_base::failure)
    > {
    > //always getting a badbit exception even if I enter Ctrl-D
    > //which I thought signified eof
    >
    > ios_base::iostate s = cin.exceptions();
    >
    > if(s&ios_base::badbit) cout<<"\n BadBit Exception";
    > else if(s&ios_base::failbit) cout<<"\n Fail Bit Exception";
    > else if(s&ios_base::eofbit) cout<<"\n EOF Bit Exception";
    > }
    >
    > try
    > {
    > //clear cin state
    > cin.clear(); //cin.clear(ios_base::badbit) causes BadBit exception
    >
    > //sort input array
    > sort(v1.begin(), v1.end());
    >
    > cout<<"\nEnter Second List:";
    >
    > //read second time
    > copy(istream_iterator<int>(cin), istream_iterator<int>(),
    > back_inserter(v1));
    > }
    > catch(ios_base::failure)
    > {
    > ios_base::iostate s = cin.exceptions();
    > if(s&ios_base::badbit) cout<<"\n BadBit Exception";
    > else if(s&ios_base::failbit) cout<<"\n Fail Bit Exception";
    > else if(s&ios_base::eofbit) cout<<"\n EOF Bit Exception";
    > }
    >
    > return 0;
    > }//endmain()
    > /*****************************************************************/
    >
    > First Time I enter some integers and then a character or ENTER & Ctrl-D
    > (which should work as EOF). I get a badbit exception (surprisingly even
    > in case of Ctrl-D I am getting bad bit and not eof) which I clear by
    > invoking cin.clear(). Now when I try to input elements from cin again
    > (second time) , I get badbit exception immediately (without taking any
    > input)..:-(
    >
    > Seemingly either cin.clear() is not resetting bad bit or bad bit has to
    > be handled differently.


    Clear is resetting the bad bit but the character or cntrl-D is still in
    the stream. So as soon as you start trying to read the second time, you
    get another exception when it tries to once again read the bad data.

    Try adding this after the clear:

    cin.ignore( numeric_limits<streamsize>::max() );



    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Mar 26, 2006
    #2
    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,102
    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,162
    Kevin Saff
    Jan 6, 2004
  3. voidstar
    Replies:
    3
    Views:
    10,091
    Mark R Rivet
    Jul 27, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,689
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,677
    Fernando
    Nov 16, 2011
Loading...

Share This Page