C++ 此字符串比较不是';行不通

C++ 此字符串比较不是';行不通,c++,string,if-statement,compare,string-comparison,C++,String,If Statement,Compare,String Comparison,因此,当我输入我想要使用的月份时,例如12月,我将722作为小时数,程序会说,“您输入的小时数不能超过720个月内的小时数”。有办法解决吗?我不想每个月都发表if声明,我觉得有一个更简单的方法。这也是我的第一个大学课程 int userPackage, userHours; //Declaring integer variables double savings, savings2, total; //Declaring double value string userMonth; cout&l

因此,当我输入我想要使用的月份时,例如12月,我将722作为小时数,程序会说,“您输入的小时数不能超过720个月内的小时数”。有办法解决吗?我不想每个月都发表if声明,我觉得有一个更简单的方法。这也是我的第一个大学课程

int userPackage, userHours; //Declaring integer variables
double savings, savings2, total; //Declaring double value
string userMonth;
cout<<"\tHello.\nEnter the number of the package you have\n1) Package A\n2) Package B\n3) Package C\n"; //Prompts the user for their package in a menu like fashion
cin>>userPackage; //gets package
if(userPackage > 3 || userPackage < 1) //Error output for numbers that don't match packages
{
    cout<<"Error, invalid choice";
    return 0;
}

cout<<"Enter the number of hours you have been online."; //Propmts the user for the number of hours they've been online
cin>>userHours; //gets hours
cout<<"Enter the month (by name): ";
cin>>userMonth;
cout<<"\n";
if(userMonth == "January","March","May","July","August","October","December")
{
    if (userHours > 744)
    {
        cout<<"The amount of hours you entered cannot exceed the amount of hours within the month 744";
        return 0;
    }
}
if(userMonth == "April", "June", "September", "November");
{
    if(userHours > 720)
    {
        cout<<"The amount of hours you entered cannot exceed the amount of hours within the month 720";
        return 0;
    }
}
if(userMonth == "February");
{
    if (userHours > 672)
    {
        cout<<"The amount of hours you entered cannot exceed the amount of hours within the month 672";
        return 0;
    }   
}
int userPackage,userHours//声明整数变量
双重储蓄,储蓄2,总计//声明双值
字符串userMonth;
coutuserPackage//获取包
if(userPackage>3 | | userPackage<1)//输出与包不匹配的数字时出错
{
库特
if(userMonth==“一月”、“三月”、“五月”、“七月”、“八月”、“十月”、“十二月”)

这与您认为的不同(即,不会将
userMonth
与每个字符串进行比较)。您可能打算编写的语句(假设您也希望使用
else,即使您的代码没有):

注意:这些也是区分大小写的比较(即,“一月”不等同于“一月”或任何其他大小写差异),最好将所有内容转换为所有小写或所有大写

if(userMonth==“四月”、“六月”、“九月”、“十一月”);

//有问题的尾随分号^

这将结束
if
语句,并无条件执行下一个块。因此,当输入
722
时,它总是大于
720
,并且您将看到消息


在“二月”的
if
逻辑中也存在同样的错误。

userMonth==“一月”、“三月”、“五月”、“七月”、“八月”、“十月”、“十二月”
这不是将字符串与多种可能性进行比较的方式。您可以将这些字符串存储在静态常量数组或集合中,并查找字符串是否在其中。
if(UsCale= =“四月”、“六月”、“九月”、“十一月”);我敢打赌,你从来没有见过这样的代码< < /Cord>语句,如在任何书、教程、网站等中所声称的C++教学。你是怎么想出这个的?你的措辞暗示“<代码>(用户月= =“一月”、“三月”、)相当于<代码>(userMonth==“一月”| | userMonth==“三月”| |
事实并非如此。感谢您的帮助。我曾尝试在网上查找解决方案,但不知道如何表达我的问题。我仍在学习所有术语。但是的,它起了作用,我能够继续编写程序。非常感谢!
if (userMonth == "January" ||
    userMonth == "March" ||
    userMonth == "July" ||
    userMonth == "August" ||
    userMonth == "October" ||
    userMonth == "December")
{
    ...
}
else if (userMonth == "April" ||
    userMonth == "June" ||
    userMonth == "September" ||
    userMonth == "November")
{
}
else if (userMonth == "February")
{
}