cece
cece

Reputation: 421

scanf and no switch case is executed in c

This code is supposed to calculate leap years. I have not finished including all the cases that calculate this, but regardless it is not going into each case but just skipping to default. I just started practicing C so explanations with detail would really help here.

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year you wish to check is a leap year\n");
    scanf(" %d", &year);

    switch(year)
    {
        case 1: //does not go into this case 
            if(year%4 == 0)
            {
                printf("leap year\n"); 
            }
            break;
        case 2: //does not go into this case
            if(year%100 == 0 && year%400 == 0)
            {
                printf("leap year\n"); 
            }
            break;
        default:
            printf("not a leap year\n");
    }

    return 0;
}

Upvotes: 1

Views: 81

Answers (4)

tadman
tadman

Reputation: 211560

The switch statement doesn't have numbered clauses like "clause #1" and "clause #2" but instead it tests year against the literal values 1 and 2. What you mean is probably:

int is_leap_year(year) {
  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

Then you can use that function in your main body of code:

if (is_leap_year(year)) {
  printf("Is leap year\n");
}
else {
  printf("Is not leap year\n");
}

By putting it into a function you can quickly test against a number of cases. You want to be sure that 1900 is not a leap year, that 2000 and 2004 are, and that 2100 is not.

Upvotes: 3

ergesto
ergesto

Reputation: 397

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year you wish to check is a leap year\n");
    scanf(" %d", &year);

    switch (year % 4)
    {
    case 0:
        if (year % 100 == 0)
        {
            printf("\"Century\" can't be leap year\n");

        }
        else
            printf("leap year\n"); 
        break;

    case 3:
        printf("Next year is a leap year.\n");

    default:
        printf("not a leap year\n");
        break;
    }
    return 0;
}

Upvotes: 0

Jacob Boertjes
Jacob Boertjes

Reputation: 971

It seems you have a slight misunderstanding about switch statements. This is your given example:

switch(year)
{
    case 1: //does not go into this case 
        if(year%4 == 0)
        {
            printf("leap year\n"); 
        }
        break;
    case 2: //does not go into this case
        if(year%100 == 0 && year%400 == 0)
        {
            printf("leap year\n"); 
        }
        break;
    default:
        printf("not a leap year\n");
}

And this is the equivalent using if statements

if (year == 1) 
{
    if(year%4 == 0)
    {
        printf("leap year\n"); 
    }
} 
else if (year == 2) 
{
    if(year%100 == 0 && year%400 == 0)
    {
        printf("leap year\n"); 
    }
} 
else 
{
    printf("not a leap year\n");
}

Each "case" is actually just comparing year to a given value. I would instead suggest using if statements to check if it is a leap year.

if(year%4 == 0)
{
    printf("leap year\n"); 
}
else if(year%100 == 0 && year%400 == 0)
{
    printf("leap year\n"); 
}
} 
else 
{
    printf("not a leap year\n");
}

Here is a resource if you want to learn more about switch statements: https://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490098

Right now, your code is checking for the user having entered 1 or 2 as the year. For any other year (e.g., 2018) it's going to jump directly to the default label.

I'd guess (but I'll admit I'm uncertain) that you wanted something more like:

switch ( year % 4 ) { 
    case 0:
        if (year % 100 == 0 && year % 400 != 0) {
            printf("Not a leap year");
            break;
        }
    default:
        printf("leap year");
}

We're not getting a whole lot out of using a switch though--it could just as well be an if.

Upvotes: 1

Related Questions