在结构数组中,为什么 strcmp 函数不适用于字符串?

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;
}

函数 fgets 可以在输入的字符串后附加您应该删除的换行符 '\n'

例如

    fgets(s[i].Gender,10,stdin);
    s[i].Gender[ strcspn( s[i].Gender, "\n" ) ] = '[=10=]';

    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;
    }

请注意,您可以将代码包含在 do-while 语句中,而不是使用 goto 语句。

同样是输入字母,用函数fgets也没什么意义,而且输入的字符串会被覆盖

您可以使用 char 类型的对象并像这样使用 scanf 的调用

char gender;

scanf( " %c", &gender );

然后

if ( toupper( ( unsigned char )gender ) == 'M' )
{
    strcpy(s[i].Gender,"Male");
}
//...

我已经修复了你的代码。有很多错误。

  • 你刷新标准输入
  • 混合使用 scanf() 和 fgets()
  • 检查“F”时缺少 else

还有其他可以改进编码的地方。我把它作为你的练习:-)

#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;
}