C ++,如果来自字符串的特定单词

c++ if specific words from string

本文关键字:单词 字符串 如果      更新时间:2023-10-16

我想在 if 语句中签入定义的字符串 (cin) 中的几个单词,以便它接受大写和小首字母的不同用法。

#include <iostream>
using namespace std;
int main()
{
string BothMods;
cout << "Are both online?" << endl;
cin >> BothMods;
if (BothMods == "Yes", "YES", "yes"{
    cout <<"Both are online" << endl; 

但是当我输入三个条件之一时,条件总是 false(否则会被执行)。如果我只使用一个(例如如果(BothMods == "是")),它可以工作。

如果要检查多个案例,则需要使用 OR 运算符单独检查每个案例。

if (BothMods == "Yes" || BothMods == "YES" || BothMods == "yes") {
    // do whatever
}