本文介绍了C ++ CIN只有布尔(0,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现来限制用户仅0或1时,程序要求布尔变量。
I'we试图这样做,但它不工作。它仍然不断地问我打字。
布尔ELE;
做{
COUT<< 电力币:\\ t的;
CIN>> ELE;
如果(CIN和放大器;&安培; ELE == 0安培;&安培; ELE == 1)破;
cin.clear();
COUT<< Neveljaven VNOS! << ENDL;
}而(真);
解决方案
克里斯指出的,你想要做的如果(CIN和放大器;及(ELE == 0 || ELE == 1 ))打破;
I wish to achieve to limit user on only 0 or 1 when program asking for boolean variable.I'we tried to do so, but it doesn't work. It still keep asking me for typing in.
bool ele;
do{
cout << "Elektro:\t";
cin >> ele;
if (cin && ele == 0 && ele == 1) break;
cin.clear();
cout << "Neveljaven vnos!" << endl;
}while(true);
解决方案
As chris points out, you want to do if (cin && (ele == 0 || ele == 1)) break;
这篇关于C ++ CIN只有布尔(0,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!