【发布时间】:2020-08-26 05:07:00
【问题描述】:
无论输入如何,我的 if 语句都会受到影响。我无法理解为什么。代码如下:
void Novice::selection()
{
char selection, shift;
cout << "Please select a section to run: A - Home Row, B - Bottom Row, C - Top Row, D - Pointer Fingers, E - Right Pinky;" << endl;
cin >> selection;
selection = toupper(selection);
if (selection != 'A' || 'B' || 'C' || 'D' || 'E') {
cout << "Invalid Input. Please select again" << endl;
cin >> selection;
}
if (selection == 'A' || 'B' || 'C') {
cout << "you're here" << endl;
}
如果输入是'A',则触发第一个if语句,如果我再放入A,第二个if语句也会触发。任何帮助将不胜感激。
【问题讨论】:
-
告诉我
||运算符的作用。 -
表达式
'B'的计算结果为字符代码 66(假设正在使用 ASCII),它是非零的,所以它是真的。如果任一操作数为真,||操作的结果为真。这意味着您的 if 条件将始终为真。 -
这能回答你的问题吗? Character check in file always returns true