c++ - C++ While语句不适用于字符串和 “or”

标签 c++ string while-loop

我是C++的新手,可以通过youtube和自己购买的一些书教自己。我无法为自己的一生弄清楚为什么第二个while语句不起作用。从数学的 Angular 来看,我认为它应该起作用。如果月份不是= 6月或7月,则执行if else语句。但是,即使我运行正确的答案,也总是运行“否则”。我觉得它与字符串有关,因此我在不使用或“||”的情况下对其进行了测试而且有效。因此,可能与组合字符串和或语句有关。关于将它们一起使用的研究也没有发现太多。谢谢您的帮助。

int main()
{
    int year;
    int day = 0;
    string month = "x";

    do
    {
        if (day == 0)
        {
            cout << "hello" << endl;
            cout << "Please Enter your B-Day as Day, Month, Year" << endl;
            cout << "day" << endl;
            cin >> day;
        }
        else
        {
            cout << "Please enter a correct day" << endl;
            cin >> day;
        }

    } while (day > 31 || day < 1);

    do
    {
        if (month == "x")
        {
            cout << "Please enter the month you were born" << endl;
            cin >> month;
        }
        else
        {
            cout << "Please Enter a correct Month." << endl;
            cin >> month;
        }
    }
    **while (month != "june" || month != "july");**


    return 0;
}

最佳答案

如果您这样做:

while (month != "june" && month != "july");

或者,
while (!(month == "june" || month == "july"));

而不是:
while (month != "june" || month != "july");

即使使用多个逻辑或,您的程序也可以正常工作。

关于c++ - C++ While语句不适用于字符串和 “or”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165667/

相关文章:

Java将int转换为十六进制并再次转换回来

c - 在 C 中的 while 循环条件中使用赋值运算符

c++ - 在函数结束后使用指向变量的指针是否安全?

c++ - 如果不需要参数,是否应该使用 argc 和 argv?

java - 如何从给定字符串获取描述中的预期输出

java - 方法未完成运行 - 无限工作

Java:while 循环中定义的 String[] 的 "cannot find symbol"错误

c++ - 头文件的多重包含

c++ - 如何在 C++ Builder 中显示更长时间的工具提示?

string - 在vc++中将 'System::String ^'转换为 'const char *'