定义布尔数据类型的变量并使用cin获取用户输入时,预期输入0或1。

huangapple go评论62阅读模式
英文:

when define variable with bool data type and we will use cin to get user input.expected 0 or 1

问题

当我写x等于3时,我超出了预期,因为我没有写零或一,但尽管如此,这个非零值隐式地转换为布尔值,所以输出是1。

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

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

当我写x等于文本"hallo"时,我也超出了预期,因为我没有写零或一,但在这种情况下,这个非零值没有隐式地转换为布尔值,所以输出是0。难道不应该预期文本"hallo"会转换为布尔值,因此输出应该是1吗?

假设输出将是1,因为文本值"hallo"被转换为布尔值,所以输出应该是1。因此,如果你告诉我cin >>从一开始就期望输入零或一的值,那么你违反了预期,所以输出是零。我会回答你说,当我输入x的值为3时,我也违反了预期,因为我应该输入零或一的值,然而输出是1,那么当我输入文本值时为什么不会发生这种情况,为什么它不会隐式转换为布尔值呢?

英文:

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 &lt;iostream&gt;
using namespace std;
int main() {
  bool x;
  cin&gt;&gt;x;

  cout &lt;&lt; x&lt;&lt; &quot;\n&quot;;
  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 &gt;&gt; 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

得分: 1

一般而言,您需要区分流上是否设置了std::boolalpha

如果没有设置,那么bool会像其他无符号整数一样对待,在这种情况下,范围从0到1(包括1)。如果我们根本无法读取任何数值(这对于任何非数值初始字符都是如此),则结果为0(即false);如果我们可以读取但值太大以至于无法表示(对于bool来说,任何大于1的数都会被转换为一个巨大的正数),则结果是整数的最大值,对于bool来说是1(即true)。在这两种情况下,流的失败位将被设置,您可以通过检查流的状态来检查它:

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

如果应用了std::boolalpha,情况就会改变 - 此时任何匹配truefalse的文本都会转换为相应的值,而任何其他文本都会导致false,同时设置了失败位。

参考 std::num_get::get,其中 operator>> 也有涉及。

英文:

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 &gt;&gt; x)
    std::cout &lt;&lt; &quot;good\n&quot;;
else
    std::cout &lt;&lt; &quot;fail\n&quot;;
std::cout &lt;&lt; x &lt;&lt; &quot;\n&quot;;

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&gt;&gt; refers to.

huangapple
  • 本文由 发表于 2023年7月17日 18:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定