0

C What is that that I'm doing wrong?I'm trying to print for the different months with dates their corresponding seasons But I keep getting the first (printf) for all of them which is Winter.

dates for each season are: Spring: March 20 - June 20 Summer: June 21 - September 21 Autumn: September 22 - December 20 Winter: December 21 - March 19

#include <stdio.h>

#include <string.h>


int main(void) {
    char inputMonth[50];
    int inputDay;

    scanf("%s", inputMonth);
    scanf("%d", & inputDay);

    /* Type your code here. */
    //winter
    if (strcmp(inputMonth, "December") && (21 <= inputDay && inputDay <= 30)) {
        printf("Winter\n");
    } else if (strcmp(inputMonth, "January") && (1 <= inputDay && inputDay <= 31)) {
        printf("Winter\n");
    } else if (strcmp(inputMonth, "February") && (1 <= inputDay && inputDay <= 29)) {
        printf("Winter\n");
    } else if (strcmp(inputMonth, "March") && (1 <= inputDay && inputDay <= 19)) {
        printf("Winter\n");
    }
    //spring
    else if (strcmp(inputMonth, "March") && (20 <= inputDay && inputDay <= 31)) {
        printf("Spring\n");
    } else if (strcmp(inputMonth, "April") && (1 <= inputDay && inputDay <= 31)) {
        printf("Spring\n");
    } else if (strcmp(inputMonth, "May") && (1 <= inputDay && inputDay <= 30)) {
        printf("Spring\n");
    } else if (strcmp(inputMonth, "June") && (1 <= inputDay && inputDay <= 20)) {
        printf("Spring\n");
    }
    //summer
    else if (strcmp(inputMonth, "June") && (21 <= inputDay && inputDay <= 31)) {
        printf("Summer\n");
    } else if (strcmp(inputMonth, "July") && (1 <= inputDay && inputDay <= 31)) {
        printf("Summer\n");
    } else if (strcmp(inputMonth, "August") && (1 <= inputDay && inputDay <= 31)) {
        printf("%sSummer\n");
    } else if (strcmp(inputMonth, "September") && (1 <= inputDay && inputDay <= 21)) {
        printf("Summer\n");
    }
    //autumn
    else if (strcmp(inputMonth, "September") && (22 <= inputDay && inputDay <= 30)) {
        printf("Autumn\n");
    } else if (strcmp(inputMonth, "October") && (1 <= inputDay && inputDay <= 31)) {
        printf("Autumn\n");
    } else if (strcmp(inputMonth, "November") && (1 <= inputDay && inputDay <= 30)) {
        printf("Autumn\n");
    } else if (strcmp(inputMonth, "December") && (1 <= inputDay && inputDay <= 20)) {
        printf("Autumn\n");
    } else {
        printf("Invalid\n");
    }

    return 0;
}

1 Answers1

3

strcmp(inputMonth, "December")

This returns true if inputMonth is different from December.

strcmp just returns a negative value,0 or a positive value if the first is less than/equal to/greater than the second argument. So, if both strings are the same, strcmp() returns 0.

If you wish to check for "December" then you need !strcmp()

Thus, if (!strcmp(inputMonth, "December"))

moi
  • 467
  • 4
  • 19