12

I am new to C++ and I was wondering how the function cin in case of a boolean data works. Let's say for instance :

bool a;
cin >> a;

I understand that if I give 0 or 1, my data a will be either true or false. But what happens if I give another integer or even a string ?

I was working on the following code :

#include <iostream>

using namespace std;

int main()
{
    bool aSmile, bSmile;
    cout << "a smiling ?" << endl;
    cin >> aSmile;
    cout << "b smiling ?" << endl;
    cin >> bSmile;
    if (aSmile && bSmile == true)
        cout << "problem";
    else
        cout << "no problem";
    return 0;
}

If I give the values of 0 or 1 for both boolean, there is no problem. But if I give another integer, here is the output :

a smiling ?
9
b smiling ?
problem

I am not asked to enter any value to bSmile, the line cin >> bSmile seems to be skipped. The same happens if I give a string value to aSmile.

What happened?

wtz
  • 426
  • 4
  • 15
Xema
  • 1,796
  • 1
  • 19
  • 28

2 Answers2

15

From cppreference:

If the type of v is bool and boolalpha is not set, then if the value to be stored is ​0​, false is stored, if the value to be stored is 1, true is stored, for any other value std::ios_base::failbit is assigned to err and true is stored.

Since you entered a value that was not 0 or 1 (or even "true" or "false") the stream set an error bit in its stream state, preventing you from performing any further input.

clear() should be called before reading into bSmile. Also, this is a good reason why you should always check if your input suceeded with a conditional on the stream itself.

David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    It might be worth adding that you can make the errors more obvious with exceptions: http://www.cplusplus.com/reference/ios/ios/exceptions/ – matsjoyce Oct 05 '14 at 14:42
  • Thanks a lot. With cin.clear before the bSmile input it works for other integers, but it's still not working for strings ! – Xema Oct 05 '14 at 15:07
  • @Xema "Not working" is very vague, you need to show the code and the behavior. – David G Oct 05 '14 at 15:27
  • I just add cin.clear() after the "cin >> aSmile" in the code above. And with a string input, the result is the same as the one without cin.clear() (you can see it above) – Xema Oct 05 '14 at 15:47
  • @Xema When you say "string input" do you mean you're providing characters as input? Is `aSmile` still a `bool`? – David G Oct 05 '14 at 15:57
  • I put "false" in aSmile (while aSmile is a boolean). – Xema Oct 05 '14 at 16:10
  • @Xema You need to enable boolean string input using `cin >> boolalpha`. – David G Oct 05 '14 at 16:25
  • Thanks, it worked. But with cin >> boolalpha i can't use both string and integers right ? Is there a way to use them both at the same time or not ? – Xema Oct 05 '14 at 17:32
  • 1
    @Xema The only way I'd know of is to override the `std::num_get` facilities and incorporate that custom functionality. Either that or read the values into a string and parse the correct value. – David G Oct 05 '14 at 17:45
0

First of all, in c++, a boolean variable can take only two values, true & false. If you want to enter the value of a boolean later in your program, you can by not initializing the variable eg, bool isMale;

later in your program, if you want to give the boolean variable 'isMale' a value, you can do so by ..

cin >> isMale;

But then make sure the value you enter is either true or false, else the program will throw an error

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 03:22