当前位置:网站首页>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:" <
边栏推荐
- sql创建表格 如图 运行完提示invalid table name 是什么原因
- gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
- metabase访问adb mysql 如何控制会话时区??
- 设置工作模式与环境(中):建造二级引导器
- 图扑软件数字孪生油气管道站,搭建油气运输管控平台
- Inverter Phase Locking Principle and DSP Implementation
- 数据表格化打印输出
- Appium swipe problem
- MySQL优化之慢日志查询
- Appium 滑动问题
猜你喜欢
随机推荐
MySQL - Detailed Explanation of Database Transactions
有点奇怪!访问目的网址,主机能容器却不行
postgres groupby merge strings
多表的查询
@RequestBody使用
【CV】OpenVINO installation tutorial
Please tell me, how to write Flink SQL and JDBC sink into mysql library and want to create an auto-incrementing primary key
BGP通过MPLS解决路由黑洞
(2022 Niu Ke Duo School 5) B-Watches (two points)
小说里的编程 【连载之二十五】元宇宙里月亮弯弯
I.MX6U-ALPHA开发板(定时器按键消抖)
Introduction to Totem Pole and Push-Pull Circuits
59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
五款优秀免费的在线抠图工具
Biotin-C6-amine|N-生物素基-1,6-己二胺|CAS:65953-56-2
数据表格化打印输出
我与csdn
Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example
HCIP 第五天
HCIP第二天







