当前位置:网站首页>52. [Bool type input any non-0 value is not 1 version reason]
52. [Bool type input any non-0 value is not 1 version reason]
2022-08-02 08:21:00 【Lee is struggling...】
[visual studio 2022 version]

[that is, the bool type is not assigned an initial value]
#include
using namespace std;
int main()
{
bool x;
cout << "Please enter the bool value manually:" << endl;
cin >> x;
cout << "The result is:" << x << endl;
return 0;
}

========================== 
[The initial value of bool is assigned]
#include
using namespace std;
int main()
{
bool x=false;
cout << "Please enter the bool value manually:" << endl;
cin >> x;
cout << "The result is:" < return 0; } #include using namespace std; int main() { bool x; cout << "Please enter the bool value manually:" << endl; cin >> x; cout << "The result is:" << x << endl; return 0; } ======================== #include using namespace std; int main() { bool x=false; cout << "Please enter the bool value manually:" << endl; cin >> x; cout << "The result is:" < return 0; } ======================== #include }
=================================
[visual studio 2015 version]

[That is, the bool type is not assigned an initial value]


[When bool is assigned an initial value (will become false)]

Why is this happening?
Because when the bool type variable is assigned through cin, if the input is "non-0, non-1", the variable value does not change.That is, the bool type variable can only be assigned 0 or 1 through cin, and the others are invalid.
How to change it?
Generally, it is judged by setting the intermediate quantity, and then indirectly assigning the bool variable.Regardless of whether x is defined or not, the bool type variable x is forcibly assigned through the int type variable a, which effectively solves the above problems.
[Forced assignment]
using namespace std;
int main()
{
bool x=false;
cout << "Please enter the bool value manually:" << endl;
int a;
cin >> a;
x = a;
cout << "The result is:" <
边栏推荐
猜你喜欢

Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example

有点奇怪!访问目的网址,主机能容器却不行

MySQL常见索引类型

牛客2022 暑期多校4 D Jobs (Easy Version)(递推优化策略)

MySQL - low level settings

Comprehensive experiment of MPLS and BGP
![MFC最详细入门教程[转载]](/img/5d/655e75f6b8a547fefe0137a9b11536.png)
MFC最详细入门教程[转载]

pnpm + workspace + changesets 构建你的 monorepo 工程

用户身份标识与账号体系实践

MGRE综合实验
随机推荐
PLSQL Developer安装和配置
(2022 Niu Ke Duo School 5) D-Birds in the tree (tree DP)
postgres groupby 合并字符串
@Repository详解
MySQL - locking mechanism
MySQL常见索引类型
解决IDEA安装安装插件慢问题
常用的云安全防护措施盘点
HCIP 第十一天
Comprehensive experiment of MPLS and BGP
MFC最详细入门教程[转载]
如何将项目部署到服务器上(全套教程)
WebForm DropDownList bind year and month respectively
如何开启mysql慢查询日志?
爬虫——爬虫初识、requests模块
论文理解:“Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution,Denoising, and “
Xilinx Constraint Study Notes - Timing Constraints
设置 height: auto 却无法触发 transition 动画的解决方案
小说里的编程 【连载之二十三】元宇宙里月亮弯弯
小说里的编程 【连载之二十五】元宇宙里月亮弯弯