简体   繁体   中英

How do I use switch case properly?

Can anyone tell me am I using switch cases correct? because when I input other number, it's output is always January.

#include <stdio.h>


int main()
{
    int month, date, year;

    printf("Enter Month Number: ");
    scanf("%d", &month);

    if (month > 12){
        printf("Invalid Month! please choose a number below 12");
    }
    else{
        printf("\nEnter Date Number: ");
        scanf("%d", &date);

        if (month = 1, 3, 5, 7, 8, 10, 12){
            if (date > 31){
                printf("\nInvalid Date! please choose a number below 31");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    switch(month){
                        case 1:
                            printf("\nJanuary %d, %d", date, year);
                            break;
                        case 3:
                            printf("\nMarch %d, %d", date, year);
                            break;
                        case 5:
                            printf("\nMay %d, %d", date, year);
                            break;
                        case 7:
                            printf("\nJuly %d, %d", date, year);
                            break;
                        case 8:
                            printf("\nAug %d, %d", date, year);
                            break;
                        case 10:
                            printf("\nOctober %d, %d", date, year);
                            break;
                        case 12:
                            printf("\nDecember %d, %d", date, year);
                            break;
                    }
                }
            }
        }
        else if (month = 4, 6, 9, 11){
            if (date > 30){
                printf("\nInvalid Date! please choose a number below 30");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    switch(month){
                        case 4:
                            printf("\nApril %d, %d", date, year);
                            break;
                        case 6:
                            printf("\nJne %d, %d", date, year);
                            break;
                        case 9:
                            printf("\nSeptember %d, %d", date, year);
                            break;
                        case 11:
                            printf("\nNovember %d, %d", date, year);
                            break;
                    }
                }
            }


        }
        else if (month = 2){
            if (date > 29){
                printf("\nInvalid Date! please choose a number below 29");
            }
            else{
                printf("\nEnter Year: ");
                scanf("%d", &year);
                if (year > 9999){
                    printf("\nPlease make sure your year is correct!");
                }
                else if (year < 1000){
                    printf("\nPlease make sure your year is correct!");
                }
                else{
                    printf("\nFebruary %d, %d", date, year);

                }
            }
        }


    }

return 0;
}

A few issues...

  1. = is the assignment operator. You want the equality operator ==
  2. if (month = 4, 6, 9, 11) is just incorrect syntax. As mentioned in the top comments, this would be done with a switch/case block or if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) .
  3. The code is needlessly verbose. Having a struct that describes the month (days and month string) and an array of it greatly simplifies the code.
  4. Prompting the user for all three values before doing checking can also simplify the code.

Here is a simplified version. It is annotated:

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

struct month {
    int days;                           // (max) number of days in month
    const char *str;                    // month string
};

struct month months[12] = {
    { 31, "January" },
    { 29, "February" },
    { 31, "March" },
    { 30, "April" },
    { 31, "May" },
    { 30, "June" },
    { 31, "July" },
    { 31, "August" },
    { 30, "September" },
    { 31, "October" },
    { 30, "November" },
    { 31, "December" },
};

// asknum -- prompt user for number within a given range
int
asknum(const char *prompt,int lo,int hi)
{
    char buf[100];
    char *cp;
    long val;

    // prompt the user
    printf("Enter %s (%d to %d): ",prompt,lo,hi);
    fflush(stdout);

    // get user's response -- exit on EOF
    if (fgets(buf,sizeof(buf),stdin) == NULL)
        exit(7);

    // check for empty string
    if (buf[0] == '\n') {
        printf("No value specified\n");
        exit(8);
    }

    // decode the value
    errno = 0;
    val = strtol(buf,&cp,10);

    // check for error detected by strtol
    if (errno != 0) {
        printf("Invalid number\n");
        exit(9);
    }

    // check for syntax error
    if (*cp != '\n') {
        printf("Invalid syntax\n");
        exit(10);
    }

    // check range of number
    if ((val < lo) || (val > hi)) {
        printf("Number out of range -- must be between %d and %d\n",
            lo,hi);
        exit(11);
    }

    return val;
}

// isleap -- decide if it's a leap year
int
isleap(int year)
{
    int leap = 0;

    do {
        if ((year % 4) != 0)
            break;

        if ((year % 400) == 0) {
            leap = 1;
            break;
        }

        if ((year % 100) == 0)
            break;

        leap = 1;
    } while (0);

    return leap;
}

int
main(void)
{
    int month, date, year;
    int days;
    int leap = 0;

    month = asknum("Month Number",1,12);

    // locate the month descriptor
    const struct month *mon = &months[month - 1];

    date = asknum("Date Number",1,mon->days);
    year = asknum("Year",1000,9999);

    // get days in the month
    days = mon->days;

    // special case for february
    if (month == 2) {
        leap = isleap(year);
        if (! leap)
            days -= 1;
    }

    // check date against number of days in the month
    if (date > days) {
        printf("%s only has %d days\n",mon->str,days);
        exit(2);
    }

    // print the date
    printf("%s %d, %d\n",mon->str,date,year);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM