-1

I was working on a problem where I need to get the birthday of someone and I need to convert the string name of the month into the integer counterpart of it, for example, if the user input "jan", the program must output 1.

I am just a beginner in C so it will be a great help if you give me some enlightenments

here is my code:

#include <stdio.h>
#include <string.h>



void birthconvert(char bm[])
{
    if (bm == "jan")
    {
        printf("01");
    }
    else if (bm == "feb")
    {
        printf("02");
    }
    else if (bm == "mar")
    {
        printf("03");
    }
    else if (bm == "apr")
    {
        printf("04");
    }
    else if (bm == "may")
    {
        printf("05");
    }
    else if (bm == "jun")
    {
        printf("06");
    }
    else if (bm == "jul")
    {
        printf("07");
    }
    else if (bm == "aug")
    {
        printf("08");
    }
        else if (bm == "sep")
    {
        printf("09");
    }
        else if (bm == "oct")
    {
        printf("10");
    }
        else if (bm == "nov")
    {
        printf("11");
    }
        else if (bm == "dec")
    {
        printf("12");
    }
}

int main() {

    char birthmonth[3];
    printf("Enter your birth month: ");
    gets(birthmonth);

    birthconvert(birthmonth);
}
EntoZeke
  • 1
  • 1

1 Answers1

1

You can't use == to test the equality of strings in C.
You have to use a function like strncmp() or strcmp(), which are defined by using man 3 strncpm command on a Linux system.
You also have the documentation here.
Be careful, you also have to include the header <string.h>

cocool97
  • 1,201
  • 1
  • 10
  • 22