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
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