如何正确使用这个函数的if语句?
How to correctly use the if statement for this function?
我似乎在程序的一个函数中的 if 语句有问题。
我希望代码行停止,直到显示正确的变量,同时输出错误消息。但是,无论输入了错误还是正确的变量,代码都会继续,并且错误消息也会随之显示。
这是带有 if 语句的代码部分
string get_class(){
string char_class;
cout<<"Choose A Character Class!"<<endl;
cout<<"The Cleric, a holy disciple! Select C."<<endl;
cout<<"The Fighter, an unstoppable warrior! Select F."<<endl;
cout<<"The Paladin, a holy warrior! Select P."<<endl;
cout<<"The Rogue, a sneaky thief! Select R."<<endl;
cout<<"The Wizard, a wizened magician! Select W."<<endl;
cin>>classSelection;
if(classSelection != "C"||"c"||"F"||"f"||"P"||"p"||"R"||"r"||"W"||"w")
cout<<"Please Enter Given Selections"<<endl;
return char_class;
}
如果给出的程序不够,或者如果此片段中的所有内容看起来杂乱无章,我深表歉意。
让我们简化一下这个例子。您的代码不起作用的原因与此代码不起作用的原因相同:
int x = 2;
if(x == 1 || 2) { ... }
这是因为||
运算符不会以这种方式连接。上面的例子计算x == 1
(false
)和用2
或(||
)的OR,如下所示:
if(false || 2) { ... } // after evaluating of x == 1
我们现在有false || 2
.在C++
中,任何非零数值的计算结果都是true
,所以我们得到false || true
,即true
。
但它似乎在这个例子中有效,对吧?
它只是看起来。将2
替换为3
,代码仍将被计算为true
,即使x
既不1
也不3
。
如何解决问题?
在小组合的情况下,应该正确提供所有表达式:
if(x == 1 || x == 2) { ... }
而不是:
if(x == 1 || 2) { ... }
因此,在您的示例中,您将需要:
if(classSelection != "C" || classSelection != "c" || classSelection != "F" || ... )
但这仍然不能做你想要的。例如,您不想知道classSelection
是否等于"C"
或不等于"c"
。你想知道它是否不等于"C"
也不等于"c"
,所以你实际上想要:
if(classSelection != "C" && classSelection != "c" && classSelection != "F" && ... )
您的示例需要一些键入。另一种方法,效率稍低,但可以说更具可读性,是将所有可能的匹配项存储在数据结构中并使用标准算法,例如std::all_of
:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
std::vector<const char*> not_accepted= {
"C", "c", "F", "f", "P", "p", "R", "r", "W", "w"
};
std::string choice = "f"; // provided by user
bool valid = std::all_of(not_accepted.cbegin(), not_accepted.cend(),
[&](auto arg){
return choice != arg;
});
std::cout << "valid? - " << std::boolalpha << valid;
}
std::all_of
取一个范围和一个谓词,如果所有元素都满足谓词,则返回true
。在这里,范围是我们的not_accepted
向量,我们的谓词表示为 lambda,它将向量的每个元素与我们的choice
字符串进行比较。
为了优化,可以用std::set
或std::unordered_set
替换std::vector
,并检查集合中是否存在元素。这将摆脱算法调用:
#include <iostream>
#include <unordered_set>
#include <string>
int main() {
std::unordered_set<const char*> not_accepted = {
"C", "c", "F", "f", "P", "p", "R", "r", "W", "w"
};
std::string choice = "f";
bool valid = not_accepted.find("f") == not_accepted.end();
std::cout << "valid? - " << std::boolalpha << valid;
}
我个人的最爱
while("CcFfPpRrWw"s.find(classSelection) == string::npos)
{
cout << "Please Enter Given Selections" << endl;
cin >> classSelection;
}
在你的原始代码中,你犯了一个菜鸟错误,即选择可能的候选人。这些将单独计算为 true,导致整个表达式始终计算为 true。
我的代码将所有选择放在一个临时字符串中,并在其中搜索输入的字符。 如果找不到字符串::npos,则返回它,从而触发 while 循环。
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复