A beginner in language c asks a question...

Asked 2 years ago, Updated 2 years ago, 28 views

We are making a program to find out what year you were born using the 7th number of resident registration numbers.

I don't know where it's wrong.

The tin ends are wrong.

Whatever number you enter, it says, "Born in the '0s."

#include <stdio.h>

int main()
{
    char id[15];
    int  year;

    printf ("resident registration number: ");
    scanf("%s", id);

    switch(id[6]) // is this part wrong? 
{  
    case 0: case 9: year = 1800; break;
    case 1: case 2: case 5: case 6: year = 1900; break;
    case 3: case 4: case 7: case 8: year = 2000; 
}
    printf ("You were born in the %d's."\n", year);

    return 0;
}

c switch문

2022-09-20 22:19

1 Answers

C language is a language that needs to be very strict with data types.

When you input id, the data type of id is an array of char, so each value is char.

By the way, the case is checking whether it is an int-type value.

Because char 'n' is a different value from int n, which is a single digit, no case statement works, and therefore automatically initialized zero is output when year is declared.

To solve the problem simply, wrap each number in the case with ' and make it into a char shape.


2022-09-20 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.