当前位置:网站首页>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:" <
边栏推荐
猜你喜欢
随机推荐
牛客2022 暑期多校4 D Jobs (Easy Version)(递推优化策略)
研发创新编码器霍尔板,引领企业高质量发展
类型“DropDownList”的控件“ContentPlaceHolder1_ddlDepartment”必须放在具有 runat=server 的窗体标记内。
学习笔记(8)DOM
论文理解:“Cross-Scale Residual Network: A GeneralFramework for Image Super-Resolution,Denoising, and “
uniapp 禁止默认返回事件
2022-08-01 第四小组 修身课 学习笔记(every day)
理论问题与工程问题的差异在哪里?
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
数据表格化打印输出
MGRE环境下的OSPF
静态路由综合实验
停止精神内耗 每日分享
小说里的编程 【连载之二十二】元宇宙里月亮弯弯
MySQL-Execution Process + Cache + Storage Engine
用C写小游戏(三子棋)
爬虫——爬虫初识、requests模块
HCIP 第十一天
MySQL - based
CSRF-Cross-site request forgery-related knowledge









