Question about bool data type

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about bool data type

Mani
Hi,

I was using bloodshed IDE that uses mingw.
I was trying a simple test program as:

int main() {

   bool x;

   cin >> x;
   cout << "x = " << x << endl;
   return 0;
}

What should be the output when the user types in 100?
I expected it to be 1, but I get some other number (say 118)..

is it a bug in the compiler? I am not able to explain this..

best, murali.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Greg Chicares
On 2011-09-22 18:57Z, Mani wrote:
>
> int main() {
>
>    bool x;

The value of this uninitialized 'x' is undefined; it may be
 - 'true'; or
 - 'false'; or
 - neither 'true' nor 'false'
[C++2003 3.9.1/6, footnote 42].

>    cin >> x;
>    cout << "x = " << x << endl;
>    return 0;
> }
>
> What should be the output when the user types in 100?

"100" evaluates to neither 'true' nor 'false', so converting it to bool
fails, and the value of 'x' is not changed [22.2.2.1.2/14]. Then 'x' is
cast to int [22.2.2.2.2/23]...

> I expected it to be 1, but I get some other number (say 118)..

...and some indeterminate integer is printed.

> is it a bug in the compiler?

No, the compiler is working correctly. Instead of "bool x;", write
    bool x = true;
or
    bool x = false;
to avoid undefined behavior. After "cin >> x;", you might add:
    if(cin.fail())
        {cout << "Input failed" << endl; return 1;}
If you want to accept integer input and convert it to bool, then
do something like:
    int i;
    cin >> i;
    bool x = (bool)i;

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Mani
Hi Greg,

thanks for your detailed response with the pointers. that was helpful.

I wonder though, why this happens..

the following works (as expected) -- that is the conversion works as
int 0 -> bool 0, and int non-0 becomes bool 1.
int y;
cin >> y;
bool x = y;

but you do:
bool x = false;
cin >> x;

it does not work.. (that is the conversion does not happen, and the
cin is flagged as failed..)

during cin, the conversion should not happen?? if possible, can you
point me to where it says so in the C++ standard?

thanks.

On Thu, Sep 22, 2011 at 4:19 PM, Greg Chicares <[hidden email]> wrote:

>> int main() {
>>
>>    bool x;
>
> The value of this uninitialized 'x' is undefined; it may be
>  - 'true'; or
>  - 'false'; or
>  - neither 'true' nor 'false'
> [C++2003 3.9.1/6, footnote 42].
>
>>    cin >> x;
>>    cout << "x = " << x << endl;
>>    return 0;
>> }
>>
>> What should be the output when the user types in 100?
>
> "100" evaluates to neither 'true' nor 'false', so converting it to bool
> fails, and the value of 'x' is not changed [22.2.2.1.2/14]. Then 'x' is
> cast to int [22.2.2.2.2/23]...
>
>> I expected it to be 1, but I get some other number (say 118)..
>
> ...and some indeterminate integer is printed.
>
>> is it a bug in the compiler?
>
> No, the compiler is working correctly. Instead of "bool x;", write
>    bool x = true;
> or
>    bool x = false;
> to avoid undefined behavior. After "cin >> x;", you might add:
>    if(cin.fail())
>        {cout << "Input failed" << endl; return 1;}
> If you want to accept integer input and convert it to bool, then
> do something like:
>    int i;
>    cin >> i;
>    bool x = (bool)i;
>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure contains a
> definitive record of customers, application performance, security
> threats, fraudulent activity and more. Splunk takes this data and makes
> sense of it. Business sense. IT sense. Common sense.
> http://p.sf.net/sfu/splunk-d2dcopy1
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe
>

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Greg Chicares
On 2011-09-22 22:29Z, Mani wrote:
[...]

> the following works (as expected) -- that is the conversion works as
> int 0 -> bool 0, and int non-0 becomes bool 1.
> int y;
> cin >> y;
> bool x = y;
>
> but you do:
> bool x = false;
> cin >> x;
>
> it does not work.. (that is the conversion does not happen, and the
> cin is flagged as failed..)
>
> during cin, the conversion should not happen??

Correct. It does not work this way:
  read from cin into a temporary integer
  convert that int to bool
  assign converted value to 'x'
This is the way it works:
  read from cin into a temporary integer
  if that integer is 1 or 0, assign it to 'x'
  else set failbit and don't assign anything to 'x'

> if possible, can you
> point me to where it says so in the C++ standard?

Okay, let me elaborate on this:

>>> What should be the output when the user types in 100?
>>
>> "100" evaluates to neither 'true' nor 'false', so converting it to bool
>> fails, and the value of 'x' is not changed [22.2.2.1.2/14].

That paragraph says in part:
  "If the value to be stored is 0 then false is stored. If the
  value is 1 then true is stored. Otherwise err|=ios_base::failbit
  is performed and no value is stored."
That's the documentation for num_get::do_get(), which is called
[27.6.1.2.2] by the extractor
  basic_istream<charT,traits>& operator>>(bool& n);
that you're using when you write "cin >> x".

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Mani
Hi Greg,

thanks once again. I have one hopefully last question..

I see that for double input stored as ints, it works fine..

    {
           int x;

           cin >> x;
           if(cin.fail()) {
                         cout << "Input failed" << endl;
           }
           cout << "x = " << x << endl;
    }

if the user enters 10.3 -- cin succeeds, and it works as expected...

can you explain why the difference??

best, murali.

On Thu, Sep 22, 2011 at 8:19 PM, Greg Chicares <[hidden email]> wrote:

> On 2011-09-22 22:29Z, Mani wrote:
> [...]
>> the following works (as expected) -- that is the conversion works as
>> int 0 -> bool 0, and int non-0 becomes bool 1.
>> int y;
>> cin >> y;
>> bool x = y;
>>
>> but you do:
>> bool x = false;
>> cin >> x;
>>
>> it does not work.. (that is the conversion does not happen, and the
>> cin is flagged as failed..)
>>
>> during cin, the conversion should not happen??
>
> Correct. It does not work this way:
>  read from cin into a temporary integer
>  convert that int to bool
>  assign converted value to 'x'
> This is the way it works:
>  read from cin into a temporary integer
>  if that integer is 1 or 0, assign it to 'x'
>  else set failbit and don't assign anything to 'x'
>
>> if possible, can you
>> point me to where it says so in the C++ standard?
>
> Okay, let me elaborate on this:
>
>>>> What should be the output when the user types in 100?
>>>
>>> "100" evaluates to neither 'true' nor 'false', so converting it to bool
>>> fails, and the value of 'x' is not changed [22.2.2.1.2/14].
>
> That paragraph says in part:
>  "If the value to be stored is 0 then false is stored. If the
>  value is 1 then true is stored. Otherwise err|=ios_base::failbit
>  is performed and no value is stored."
> That's the documentation for num_get::do_get(), which is called
> [27.6.1.2.2] by the extractor
>  basic_istream<charT,traits>& operator>>(bool& n);
> that you're using when you write "cin >> x".
>
> ------------------------------------------------------------------------------
> All of the data generated in your IT infrastructure is seriously valuable.
> Why? It contains a definitive record of application performance, security
> threats, fraudulent activity, and more. Splunk takes this data and makes
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2dcopy2
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe
>

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Tor Lillqvist
If you want to store values like small integers 0..100, why on earth
do you insist on using the bool type then, which has been introduced
specifically for Boolean values false and true, nothing else? Are you
confusing bool with char?

--tml

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Question about bool data type

Greg Chicares
In reply to this post by Mani
On 2011-09-23 00:54Z, Mani wrote:

>
> I see that for double input stored as ints, it works fine..
>
>     {
>            int x;
>
>            cin >> x;
>            if(cin.fail()) {
>                          cout << "Input failed" << endl;
>            }
>            cout << "x = " << x << endl;
>     }
>
> if the user enters 10.3 -- cin succeeds, and it works as expected...

It should display "10".

The reason is not that it
  reads 10.3 as a floating-point number (it doesn't);
  converts that floating-point value to an integer (again, no); and
  prints the integer.
That's not what's happening. Instead, it
  reads "1" and "0", and stores them in a buffer;
  then reads "." and stops reading there because a decimal point
    cannot be part of an integer literal; then
  converts buffer contents "10" to an integer, and prints it; and
  leaves ".3" on the stream.

> can you explain why the difference??

For std::cin >> some_int, input is read and converted as if by
scanf(): i.e., characters are read one at a time, stopping at the
first character that can't be part of an int value; any remaining
characters are left on the input stream; and the characters that
have been read (before the first invalid one) are converted to an
int if possible; otherwise (e.g. if it's too large) failbit is set.

For std::cin >> some_bool, a long int is extracted as above; if
its value is either 0 or 1, then that value is assigned to the bool;
otherwise, failbit is set.

In both cases, int and bool, the operation fails if the destination
cannot represent the result of converting the extracted characters
to an integer. In the case of bool, that excludes everything but one
and zero.

If the stream contains "127.0.0.1", then "127" is extracted from
the stream, and ".0.0.1" is not. This is a valid int, but not a
valid bool. OTOH, if the stream contains "1x" or "0x", then
std::cin >> some_bool assigns values of 1 or 0 respectively to the
bool; but if it contains "10x", then nothing is assigned to the bool.
It is prudent to check the stream state after such operations.

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe