471,585 Members | 1,157 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

cin.clear() does not work

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

Mar 25 '06 #1
1 6023
In article <11*********************@g10g2000cwb.googlegroups. com>,
"AD" <an*************@gmail.com> 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.
Mar 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by TaiwanNoWhere | last post: by
3 posts views Thread by Kyle Kolander | last post: by
22 posts views Thread by mp | last post: by
9 posts views Thread by silversurfer | last post: by
5 posts views Thread by madhu | last post: by
4 posts views Thread by shapper | last post: by
9 posts views Thread by Jess | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.