Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: istream read failure: integral values as bool

Reply
Thread Tools

Re: istream read failure: integral values as bool

 
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      03-21-2011

..... including comp.lang.c++

---------- Forwarded message ----------


Consider the below code that I came across recently:

#include <iostream>
int main ()
{
* bool a=false;
* std::cout << "insert 0 or 1:";
* std::cin >> a;
* if (a==true)
* {
* * std::cout << "a is true";
* }
* else
* {
* * std::cout << "a is false";
* }

}

If you run the above code and provide input as say, 8, the code still
returns back saying a is false. I get this behavior with Visual Studio
2008. I would have thought that the integral value should get
implicitly converted to a boolean. In fact, when I debugged the code
in VC++, I do see that it is able to get the value of the user input
(i.e. but then there is a certain check which then fails and
results in setting of the failbit of the stream. If I check the status
of the cin stream post that read, I see the failbit set which means
the read had failed.

relevant bit of VC++ code (file: xlocnum, function: do_get)
Code:
* * * * * * * * * * * * { * * * // get zero or nonzero integer
* * * * * * * * * * * * char _Ac[_MAX_INT_DIG], *_Ep;
* * * * * * * * * * * * int _Errno = 0;
* * * * * * * * * * * * const unsigned long _Ulo = ::_Stoulx(_Ac,
&_Ep,
* * * * * * * * * * * * * * * * _Getifld(_Ac, _First, _Last,
_Iosbase.flags(),
* * * * * * * * * * * * * * * * * * ** _Iosbase.getloc()), &_Errno);
* * * * * * * * * * * * if (_Ep != _Ac && _Errno == 0 && _Ulo <= 1)
* * * * * * * * * * * * * * * * _Ans = _Ulo;
* * * * * * * * * * * * }

The check _Ulo <= 1 is what rejects it. _Ulo is an unsigned long which
does get successfully evaluated to 8 (the input). So, it is basically
only allowing any other inputs than 0 and 1 via the stream. Could this
be a bug in VC++? I am not sure but I would have thought that it would
have considered any non-zero value as true. Because, if I just simply
set the value of boolean as 8 instead of reading from the stream, the
automatic conversion rules kick in and the code works as expected.

Any views on this as to what the standard says because of which the
above is considered a failure? Thanks for your help.
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      03-21-2011
Abhishek Padmanabh wrote:
> .... including comp.lang.c++
>
> ---------- Forwarded message ----------
>
>
> Consider the below code that I came across recently:
>
> #include <iostream>
> int main ()
> {
> bool a=false;
> std::cout << "insert 0 or 1:";
> std::cin >> a;
> if (a==true)
> {
> std::cout << "a is true";
> }
> else
> {
> std::cout << "a is false";
> }
>
> }
>
> If you run the above code and provide input as say, 8, the code
> still returns back saying a is false. I get this behavior with
> Visual Studio 2008. I would have thought that the integral value
> should get implicitly converted to a boolean. In fact, when I
> debugged the code in VC++, I do see that it is able to get the
> value of the user input (i.e. but then there is a certain check
> which then fails and results in setting of the failbit of the
> stream. If I check the status of the cin stream post that read, I
> see the failbit set which means the read had failed.
>
> relevant bit of VC++ code (file: xlocnum, function: do_get)
> Code:
> { // get zero or nonzero integer
> char _Ac[_MAX_INT_DIG], *_Ep;
> int _Errno = 0;
> const unsigned long _Ulo = ::_Stoulx(_Ac,
> &_Ep,
> _Getifld(_Ac, _First, _Last,
> _Iosbase.flags(),
> _Iosbase.getloc()), &_Errno);
> if (_Ep != _Ac && _Errno == 0 && _Ulo <= 1)
> _Ans = _Ulo;
> }
>
> The check _Ulo <= 1 is what rejects it. _Ulo is an unsigned long
> which does get successfully evaluated to 8 (the input). So, it is
> basically only allowing any other inputs than 0 and 1 via the
> stream. Could this be a bug in VC++? I am not sure but I would have
> thought that it would have considered any non-zero value as true.
> Because, if I just simply set the value of boolean as 8 instead of
> reading from the stream, the automatic conversion rules kick in and
> the code works as expected.
>
> Any views on this as to what the standard says because of which the
> above is considered a failure? Thanks for your help.


No, it is not a bug. If the value read is invalid for the type, the
variable is left unchanged.

The stream would then have its fail() function return true.



Bo Persson




 
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
Does the C++ standard define the global function of " istream&operator >>(istream& in, string& str); "? xmllmx C++ 5 06-15-2010 11:57 AM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Reading from an istream to a bool vector Nomak C++ 4 08-16-2004 05:57 PM
How much was read during istream::read ? Steve C++ 6 05-13-2004 05:54 PM



Advertisments