473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 6191
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: TaiwanNoWhere | last post by:
// case 1 int Option = 0; for(;;) { cin >> Option; switch(Option) { case 1:
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
22
by: mp | last post by:
i have a python program which attempts to call 'cls' but fails: sh: line 1: cls: command not found i tried creating an alias from cls to clear in .profile, .cshrc, and /etc/profile, but none...
9
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
9
by: silversurfer | last post by:
Hello world, I was just wondering why the clear-line in my Makefile does not work: main:main.o $(ADDOBJS) clear @g++ -o main $(LDFLAGS) $+ As far as I understood it, it should be possible to...
0
by: dpierson | last post by:
I have just converted one of my applications from .Net 1.1 / VS2003 to ..Net 2.0 / VS2005. I'm now seeing some different behavior in my app. Here's a simple case description: Form 1: Stock...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
4
by: shapper | last post by:
Hello, Consider the following design: <div id="container"> <div id="A" class="A"></div> <div id="B" class="B"></div> </div> And the CSS classes:
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.