0

When I write that x is 3 I thus exceeded expectations because I did not write a zero or a 1 but nevertheless this non-zero value is implicitly converted into a Boolean value so ouput is 1.

#include <iostream>
using namespace std;
int main() {
  bool x;
  cin>>x;

  cout << x<< "\n";
  return 0;
}

When I write that x is text hallo I thus exceeded expectations because I did not write a zero or a 1 but in this case this non-zero value is not implicitly converted into a Boolean value so output is 0 Isn't it supposed this text hallo is converted to boolean value so output is 1.

It is assumed that the output will be 1 because the text value hallo is converted to boolean value so output is 1. So if you told me that the cin >> expects from the beginning to enter a value of zero or one .. then you violated the expectations, so the output is zero. I will reply to you and say that when I entered that the value of x is 3, I also violated expectations because I was supposed to enter a value of one or zero, and yet the output was one, so why did this not happen when I entered the text value and why was it not implicitly converted to a boolean value

  • 1
    Check state of `std::cin`... – Jarod42 Jul 17 '23 at 11:02
  • `if (not (cin>>x)) throw std::runtime_error("input error");` – Eljay Jul 17 '23 at 11:06
  • if you want to read numbers and convert them to `bool` it will be much easier to read numbers and then convert them to `bool` – 463035818_is_not_an_ai Jul 17 '23 at 11:07
  • `std::cin` won't be able to convert the value and thus it intialises `x` to zero (like any other integer on invalid input – or leaves it unchanged, if the standard applied is old enough...) and enters error state. – Aconcagua Jul 17 '23 at 11:08
  • similar question about entering a string for a `bool` https://stackoverflow.com/q/76628130/4117728. By any chance, did you ask that question? – 463035818_is_not_an_ai Jul 17 '23 at 11:15
  • @463035818_is_not_an_ai Actually `std::cin` [should be able](https://godbolt.org/z/rhEvKo6oT) to handle numeric input, and on any value other than 0 or 1 act as if the value was out of range, at least as long as `std::boolalpha` is not set on the stream... – Aconcagua Jul 17 '23 at 11:22
  • 1
    You are confusing unrelated systems. The rules for converting string literals and pointers to Boolean values apply to **code**, but your question is about **user input**. There is no reason that these two different systems have to follow similar rules, and indeed they don't. – john Jul 17 '23 at 11:35
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)… – Aconcagua Jul 17 '23 at 11:37

1 Answers1

1

In general: You need to differentiate, is std::boolalpha set on the stream or not?

If not, then bool is treated like any other unsigned integral, in given case with range from 0 up to including 1. If we cannot read any numeric value at all (which is the case for any non-numeric initial character) then the result set is 0 (i.e. false); if we can read but the value is too large to be represented (which for bool any number > 1 – and negative numbers will be converted to a huge positive one) the result is the maximum value of the integral, which for bool is 1 (i.e. true). In both cases the fail-bit of the stream will be set, and you can check it by checking the stream state:

bool x;
if(std::cin >> x)
    std::cout << "good\n";
else
    std::cout << "fail\n";
std::cout << x << "\n";

Matter changes if you apply std::boolalpha – then any text matching true or false is converted to the corresponding value while any other text results in false, again with the fail-bit set.

See std::num_get::get, which operator>> refers to.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59