This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: cin/streambuf-problem
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Subject: Re: cin/streambuf-problem
- From: "R. Sinoradzki" <sinoradz at student dot uni-kl dot de>
- Date: Sun, 20 May 2001 16:27:54 +0200
- References: <3B0599E7.9604B8FE@student.uni-kl.de>
Hi,
After reading a bit and digging in the STL I must admit,
that I perhaps misunderstood something.
The following method solves my problem:
cin.ignore( std::numeric_limits<std::streamsize>::max() ,'\n');
I thought a bit different of the underflow()-method in streambuf
and fstream.
I thought, that if an underflow occurs, the streambuffer is filled
to it's maximum size or at least with the data that is available,
if this is less.
In this case in_avail() defined as (egptr() - gptr()) would return a
bigger value.
Regards,
Ralf
"R. Sinoradzki" wrote:
>
> Hi,
>
> I have this little program. It does not behave as
> expected with gcc-3.0 and it's stdlib.
> If I type for example "hello" the loop runs 5 times,
> before the next 'cin' because 'cin.rdbuf()->in_avail()'
> reports only 1.
>
> With gcc-2.95.4 cin.rdbuf()->in_avail() returns 6
> and it works as expected.
>
> My gcc-3.0pre is an about 10 days old CVS-version. Perhaps
> this is a well-known problem.
>
> bye, Ralf
>
> /////////////////////////////////////////////////////////////
>
> #include <fstream>
> #include <iostream>
>
> using namespace std;
>
> int main(){
> int input=0;
> do{
> if (cin.fail()){
> cin.clear();
> cin.ignore(cin.rdbuf()->in_avail(),'\n'); // seems to bug
> cout<<"bad input." << endl;
> }
> cout<<"enter a number: ";
> cin>>input;
> cout << input << endl;
> }
> while(input!=3);
> return 0;
> }
>
> //////////////////////////////////////////////////////////