【问题标题】:c++ if specific words from stringc ++如果来自字符串的特定单词
【发布时间】:2017-01-13 11:41:40
【问题描述】:

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

#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; 

...

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

【问题讨论】:

    标签: c++ string if-statement cin


    【解决方案1】:

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

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

    【讨论】:

    • 如果成功了,别忘了把它标记为答案。谢谢:)
    • @VictorTran 因为那个 OP 必须等待 15 分钟冷却 :)
    • @Rakete1111 哦,当然……我忘了 :)
    • 我建议将BothMods 转换为小写(或大写),然后检查"yes"(或"YES")。
    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2015-10-30
    • 2016-12-06
    • 1970-01-01
    • 2012-07-28
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多