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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…