简体   繁体   English

cin对bool的意外行为

[英]Unexpected behaviour of cin for bool

I am trying to take input in bool type array using cin . 我正在尝试使用cin输入布尔类型数组。 If input is given like 0111100010001000 it instead of running for all iteration ( In my input it is 16 ) it terminates and print some garbage values, But if input is given like 0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 it works as expected. 如果输入像0111100010001000这样给出,而不是运行所有迭代(在我的输入中是16),它将终止并打印一些垃圾值,但是如果输入像0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0它按预期工作。

#include<cstdio>
#include<cstring>
#include<iostream>

#define FRND 2001

using namespace std;

int main(){
    bool mutualFriend[FRND][FRND];
    int noOfFriends = 0;
    cin >> noOfFriends;
    for (int i = 0; i < noOfFriends ; i++){
        for (int j = 0; j < noOfFriends; j++){
            cin >> mutualFriend[i][j];
        }
    }
    for (int i = 0; i < noOfFriends ; i++){
        for (int j = 0; j < noOfFriends; j++){
            cout << mutualFriend[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

cin.clear() can solve my problem. cin.clear()可以解决我的问题。

Please explain why loops are skipped in the first scenario. 请解释为什么在第一种情况下跳过循环。

The way operator>> parses input for a bool argument is specified in §22.4.2.1.2 [facet.num.get.virtuals]/p6 of the standard: 标准的§22.4.2.1.2[facet.num.get.virtuals] / p6中指定了operator>>解析bool参数输入的方式:

If (str.flags()&ios_base::boolalpha)==0 then input proceeds as it would for a long except that if a value is being stored into val , the value is determined according to the following: If the value to be stored is 0 then false is stored. 如果(str.flags()&ios_base::boolalpha)==0则输入将long ,除非将值存储在val ,该值根据以下确定:如果要存储的值为0然后存储false If the value is 1 then true is stored. 如果值为1则存储true Otherwise true is stored and ios_base::failbit is assigned to err. 否则,将存储true并将ios_base::failbit分配给err。

Therefore, if you give it 0111100010001000 it will first try to parse it as a long , giving you a large number (that obvious isn't 1 ). 因此,如果给它0111100010001000 ,它将首先尝试将其解析为long ,从而为您提供一个很大的数字(显然不是1 )。 Then the second step of the processing causes true to be stored into the bool and failbit to be set. 然后,处理的第二步将true存入bool并设置failbit

cin has space as default delimiter so when you do read from the value 10101.. it regards it as just a big int. cin的默认定界符为空格,因此当您从值10101中读取时,它将视为一个大整数。

Instead use .get() to read a single character 而是使用.get()读取单个字符

for (int i = 0; i < noOfFriends ; i++){
    for (int j = 0; j < noOfFriends; j++){
        mutualFriend[i][j] = (cin.get() == '1');
    }
}

TC has explained the way streaming in to a bool works, summarily a long is consumed, 0 -> false , 1 -> true , otherwise true but with failbit set. TC解释了流向bool工作方式,总共消耗了long0 > false1 > true ,否则为true但设置了failbit

For general numeric input, C++ has std::dec for decimal input, std::oct for octal (base 8), std::hex for hexadecimal (base 16), but strangely nothing for binary. 对于常规数字输入,C ++具有用于十进制输入的std::dec ,用于八进制(基数8)的std::oct ,用于十六进制(基数16)的std::hex ,但是奇怪的是对于二进制数则没有。 It is not possible to read a multi-digit binary representation of a number directly into an integral type. 无法将数字的多位二进制表示形式直接读取为整数类型。

What you'll have to do is read a character at a time, then convert to binary yourself: 您要做的就是一次读取一个字符,然后自己转换为二进制:

`char c;`

...

    if (cin >> c && (c == '0' || c == '1'))
        mutualFriend[i][j] == c != '0';
    else
        throw std::runtime_error("failure to parse binary digit from stream");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM