为什么我这个显示的是0

#include
#include
#include

using namespace std;

const int months[] = {
0,31,28,31,30,31,30,31,31,30,31,30,31
};//该数组用来存储每一个月的天数

bool check(string str) {
for (int i = 0; i + 2 < str.size(); i++)
{
if (str[i + 1] == str[i] + 1 && str[i + 2] == str[i] + 2)//表示这是一个顺子
return true;
return false;
}
}

int main()
{
int year = 2022, month = 1, day = 1;
//枚举一年365天

int res = 0;//表示天数
for (int i = 0; i < 365; i++)
{
    //把当前日期转化成一个八位的数字串
    char str[10];
    sprintf(str, "%04d%02d%02d", year, month, day);

    if (check(str))
    {
        res++;
        cout << str << endl;
    }

    if (++day > months[month])//如果发现天数加1之后大于当前月份的天数调用这个数组
    {
        day = 1;
        month++;
    }
}

cout << res << endl;

return 0;

}

bool check里的return false;放到循环外面

首先,你那个months数组内12个月元素是不是多了一个0;然后就是你check函数中,直接使用“==”来比较判断char类型与char+int类型。这是恒不等的呀。然后check一直为false那么res不会增加可不就是一直是0输出吗?

建议关注一下字符比较大小知识点,以及数字字符转数字知识点