Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cin.clear() does not work

Reply
Thread Tools

cin.clear() does not work

 
 
AD
Guest
Posts: n/a
 
      03-25-2006
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

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      03-26-2006
In article <(E-Mail Removed). com>,
"AD" <(E-Mail Removed)> 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.
 
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
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Why does post or pre incremenent or decrement does not work inside a sizeof operator? Tarun C Programming 5 07-14-2005 03:58 PM
Why does this (very simple piece of) code does not work? jblazi Python 5 08-16-2004 01:30 PM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM



Advertisments