当前位置:网站首页>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:" <
边栏推荐
- ROS file system and related commands
- 五款优秀免费的在线抠图工具
- Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!
- 读入、输出优化
- PanGu-Coder:函数级的代码生成模型
- Probability Theory and Mathematical Statistics
- Buried development process
- 设置工作模式与环境(中):建造二级引导器
- Fatal error compiling: 无效的目标发行版: 11
- 用C写小游戏(三子棋)
猜你喜欢

HCIP第二天

图扑软件数字孪生油气管道站,搭建油气运输管控平台

WebForm DropDownList bind year and month respectively

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

原型模式

MySQL - low level settings

WebRTC系列-SDP之编码信息收集

Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!

redis的安装与应用

MySQL - locking mechanism
随机推荐
基于PyTorch的flappy bird游戏
Inverter insulation detection detection function and software implementation
多表的查询
Biotinyl Cystamine|CAS:128915-82-2|生物素半胱胺
小说里的编程 【连载之二十五】元宇宙里月亮弯弯
按键控制流水灯(计时器)
Kind of weird!Access the destination URL, the host can container but not
WebRTC系列-SDP之编码信息收集
设置 height: auto 却无法触发 transition 动画的解决方案
研发创新编码器霍尔板,引领企业高质量发展
HCIP 第十一天
flutter解决键盘和输入框不适配问题
数据表格化打印输出
Comprehensive experiment of MPLS and BGP
Mysql各个大版本之间的区别
【Network】IP, subnet mask
爬虫——爬虫初识、requests模块
@FeignClient configuration参数配置
playwright 爬虫使用
Hack The Box - File Transfers Module详细讲解中文教程