1

I am doing a little experiment with boolean data type in cpp. here is my code:

bool **darray;
darray=new bool* [3];
int i=0;
while(i<3)
{
    darray[i]=new bool [5];
    i++;
}
int m,n;
for(m=0;m<3;m++)
{
    for(n=0;n<5;n++)
    {
        cin>>darray[m][n];
    }

}
for(m=0;m<3;m++)
{
    for(n=0;n<5;n++)
    {
        cout<<darray[m][n]<<"\t";
    }
    cout<<endl;
}

I know that for any non-zero input the result stored will be 1. But soon as I enter any number greater than 1, the number stored in array is 1, rest of the elements of the array is set to 0 and the for loops halts there. e.g If I enter the number 95 at first iteration of for loop, the output is :

95 
1   0   0   0   0   //
0   0   0   0   0   // This is output
0   0   0   0   0   //

Please suggest me why this is happening. Thanks in Advance.

1
  • 1
    When all your arrays are of static size, don't use dynamic allocation. If you are using c++11 use std::array else "normal" arrays like bool array[3][5];. If you need arrays of dynamic size, consider std::vector. You already have four memory leaks in your shown code. Commented Dec 17, 2014 at 8:44

3 Answers 3

1

This is because state of std::cin is getting corrupted ( Or more specifically it's fail bit is set) as expected output (integer) doesn't match given data type (bool). So, after reading 95, you can't read further unless you clear that flag

This is good example where you should check the state of stream after/before reading data from it.

Sign up to request clarification or add additional context in comments.

6 Comments

It's not getting corrupted. It just has failbit set.
@MikeSeymour I believe it's same as you can't read further without clearing it...
@MikeSeymour actually I think it is bad bit, not fail.
OK, if that's what you mean by "corrupted", then I suppose you're right. That's rather an odd use of the word though; it would usually mean that the object is in an unrecoverably invalid state.
@IvayloStrandjev: No, it's failbit (see C++11 22.4.2.1.2 /6). In general, failbit indicates that an operation failed but the stream is still usable after clearing (as it is here), while badbit indicates an unrecoverable error.
|
1

The reason for this is that reading a bool from the number 95 raises the fail bit of cin(You can verify that by printing cin.fail() at the end of your code). This means that the value passed when reading a bool value is not a proper format for a bool. All read operations will read nothing, because reading has already 'failed'. In essence 95 is not a valid bool value and cin does not know how to read it and could not possibly know what to do from this moment on(as it can not know if its state is sane).

Comments

0

95 is no valid input for reading a bool using istream (which cin is).

To detect such an error, write:

if (!(cin >> darray[m][n])) {
    cerr >> "This is no bool!" >> endl;
    return EXIT_FAILURE;
}

Or recover in some way from the error by repeating trying to read the input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.