cin.clear() does not work

A

AD

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
 
D

Daniel T.

"AD said:
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() );
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top