日期之间的天数不会编译

Days between dates will not compile

我一直在努力让它发挥作用。我需要输入 2 个日期 (MM DD),然后让程序告诉我这 2 个日期之间的天数。但出于某种原因,当我尝试使用第 2 个月(2 月)时,我不认为它的注册表明它只有 28 天。此外,当我输入相同的日期时,我无法让它说“0”。请帮忙谢谢

#include <stdio.h>
#include <stdlib.h>

//constructs dates for calculation
struct date{
    int month;
    int day;
};//end date

int main()
{
    struct date first, second; //creates 2 dates to calculate
    int finalDays = 0;
    int total = 0;
    int i = 0;
    int valid=0;

    printf("Enter first date \n");
    scanf("%d %d", &first.month, &first.day); //user input: first date
    if (first.month == 1||3||5||7||8||10){
       if(first.day > 31){
        printf("Invalid Day\n");
        valid += 1;
       }
    }
    else if (first.month == 4||6||9||11 ){
        if (first.day > 30){
         printf("Invalid Day\n");
         valid += 1;
        }
    }
    else if (first.month == 2){
        if(first.day > 28){
         printf("Invalid Day");
         valid += 1;
        }
    }

    printf("Enter second date\n");
    scanf("%d %d", &second.month, &second.day); // user input: second date
    if (second.month == 1||3||5||7||8||10){
       if(second.day > 31){
        printf("Invalid Day\n");
        valid += 1;
       }
    }
    else if (second.month == 4||6||9||11 ){
        if (second.day > 30){
         printf("Invalid Day\n");
         valid += 1;
        }
    }
    else if (second.month ==2){
        if(second.day > 28){
         printf("Invalid Day");
         valid += 1;
        }
    }

    if (first.month == second.month && first.day == second.day){
        printf("Days between dates: 0");
        valid += 1;
    }

    //Prints statement if month is invalid
    if(first.month > 12 || second.month > 12 || first.month<1 || second.month<1){
        printf("Invalid Date: Invalid month");
    }

    //Prints statement if second date precedes first
    if(second.month<first.month){
        printf("Invalid. Second date cannot precede first date.");
    }
    if (second.month==first.month && second.day<first.day){
        printf("Invalid. Second date cannot precede first date.");
    }

    if(first.month==second.month){
        finalDays = (second.day - first.day);
        printf("Days between dates: %d", finalDays);
        valid+=1;
    }

    if(first.month == 1||3||5||7||8||10||12)         // Days remaining in first month
        total = 31 - first.day;
    else if(first.month == 4||6||9||11)
        total = 30 - first.day;
    else if(first.month == 2)
        total = 28 - first.day;

    for(i = first.month + 1; i < second.month; i++)
    {
      if(i == 3||5||7||8||10||12)
          total += 31;
      else if(i == 4||6||9||11)
          total += 30;
    }
    total += second.day;

    if(valid == 0){
    printf("First date: %d %d \n", first.month, first.day);
    printf("Second date: %d %d \n", second.month, second.day);
    printf("Days between dates: %d", total);
    }

    return 0;
} //end main
if (first.month == 1||3||5||7||8||10){

这不会执行您希望它执行的操作。它将被评估为 (first.month == 1)||3||5||7||8||10,这将对所有非零月份评估为真。

这最好写成案例陈述;

switch (first.month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
        /* Handle 31 day months */
        ...
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        /* Handle 30 day months */
        ...
        break;
    case 2:
        /* Handle February */
        ...
        break;
    default:
        /* Handle invalid month */
        break;
}