Search code examples
ccomparisonc-stringsfgetsstrcmp

In Array of Structure why strcmp function is not working for string?


I want the user to enter "M" for Male or "F" for Female and the single character must be replaced by Male or Female but here the strcmp function is not working everytime time it is showing "Please enter correct value for Gender!!".

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

struct student{
    char name[50];
    int Roll_No;
    char Gender[10];
};

int main()
{
    int size;
    printf("Enter the size for number of students you want to store:");
    scanf("%d",&size);
    struct student s[size];
    int i;
    for(i=0;i<size;i++)
    {
        printf("Enter the name of student s[%d]:",i);
        fflush(stdin);
        fgets(s[i].name,50,stdin);
        printf("Enter the Roll Number of student s[%d]:",i);
        scanf("%d",&s[i].Roll_No);
        gen:printf("Enter your Gender(M-Male F-Female) for student s[%d]:",i);
        fflush(stdin);
        fgets(s[i].Gender,10,stdin);
    
        if(strcmp(s[i].Gender,"M")==0)
        {
            strcpy(s[i].Gender,"Male");
        }
        if(strcmp(s[i].Gender,"F")==0)
        {
            strcpy(s[i].Gender,"Female");
        }
        else
        {
            printf("Please enter correct value for Gender!!\n");
            goto gen;
        }
    }
    printf("\nInformation entered of Students:\n");
    for(i=0;i<size;i++)
    {
        printf("Name of student s[%d]:%s",i,s[i].name);
        printf("Roll Number of student s[%d]:%d\n",i,s[i].Roll_No);
        printf("Gender of student s[%d]:%s\n",i,s[i].Gender);
        
    }
    return 0;
}

Solution

  • I have fixed your code. There was a number of errors.

    • You flush stdin
    • You mix scanf() and fgets()
    • Missing else when checking for "F"

    There are other areas where to code could be enhanced. I let it as an exercise for you :-)

    #include<stdio.h>
    #include<string.h>
    
    struct student {
        char name[50];
        int Roll_No;
        char Gender[10];
    };
    
    int main()
    {
        int size;
        struct student s[100];
        int i;
        char buf[100];
        printf("Enter the size for number of students you want to store:");
        fgets(buf, 100, stdin);
        sscanf(buf, "%d", &size);
        for (i = 0; i < size; i++)
        {
            printf("Enter the name of student s[%d]:", i);
            fgets(s[i].name, 50, stdin);
            printf("Enter the Roll Number of student s[%d]:", i);
            fgets(buf, 10, stdin);
            sscanf(buf, "%d", &s[i].Roll_No);
        gen:printf("Enter your Gender(M-Male F-Female) for student s[%d]:", i);
            fgets(buf, 10, stdin);
            sscanf(buf, "%s", s[i].Gender);
    
            if (strcmp(s[i].Gender, "M") == 0)
            {
                strcpy(s[i].Gender, "Male");
            }
            else if (strcmp(s[i].Gender, "F") == 0)
            {
                strcpy(s[i].Gender, "Female");
            }
            else
            {
                printf("Please enter correct value for Gender!!\n");
                goto gen;
            }
        }
        printf("\nInformation entered of Students:\n");
        for (i = 0; i < size; i++)
        {
            printf("Name of student s[%d]:%s", i, s[i].name);
            printf("Roll Number of student s[%d]:%d\n", i, s[i].Roll_No);
            printf("Gender of student s[%d]:%s\n", i, s[i].Gender);
    
        }
        return 0;
    }