试图理解我的代码中的问题

输入数据在Init函数中对我有用。但是,一旦我输入了一些名称并询问了现有名称或不存在名称的分数,它总是会返回名字的等级

我创建两个数组。名称之一,等级之一。 检查它们是否符合我们定义的所有“专有名称”条款。 使用另一个名称并检查它是否在数据库中。如果是,则返回分数,如果不返回-1

''''
int Data_duplication(char* temp, char** names, int line);
void Init(char*** names, int** grades, int* size);
int Find(char** names, int* grades, int size, char* name);
void FreeAll(char*** names, int** grades, int size);
int main()
{
    char** Names = NULL;
    int* Grades = NULL;
    int size,grade;
    char name[SIZE] = { 0 };
    Init(&Names, &Grades, &size);
    printf("Enter a student name\n");
    scanf("%s",name);
    grade=Find(Names, Grades, size, name);
    printf("%d", grade);
    FreeAll(&Names, &Grades, size);
    return 0;

}
void Init(char*** names, int** grades, int* size)
{
    int i, j, flag;
    int strlengh;
    char temp[SIZE] = { 0 };
    printf("Enter number of students\n");
    scanf("%d", size);
    *names = (char**)malloc((*size) * sizeof(char*));
    if (!(*names))
    {
        printf("Error");
        return;
    }
    *grades = (int*)malloc((*size) * sizeof(int));
    if (!*grades)
    {
        printf("Error");
        return;
    }
    for (i = 0;i < *size;i++)
    {
        printf("Enter a name\n");
        scanf("%s", temp);
        strlengh = strlen(temp);
        do
        {
            flag = 1;
            if (strlen(temp) > 20)//if it longer then it should be
                flag = 0;
            if (temp[0] > 'Z' || temp[0] < 'A') //start with capital letter
                flag = 0;
            for (j = 1;temp[j] != '\0';j++)//all the letter is a lower case letters except from the first
            {
                if (temp[j] > 'z' || temp[j] < 'a')
                {
                    flag = 0;
                    break;
                }
            }
            if (Data_duplication(temp, *names, i))//if its not a name that already entered
            {
                flag = 0;
            }
            if (flag)//if the name is ok
            {
                (*names)[i] = (char*)malloc((strlengh + 1) * sizeof(char));
                if (!(*names)[i])
                {
                    printf("Error");
                    return;
                }
                strcpy((*names)[i], temp);
            }
            else//if somthing wrong
            {
                printf("Bad name,try again.\n");
                scanf("%s",temp);
            }
        } while (!flag);

        printf("Enter grade\n");
        scanf("%d", (*grades + i));
        while ( *(*grades + i) < 0 || *(*grades + i) > 100)//if the grade between 0 to 100
        {
            printf("Bad grade,try again.\n");
            scanf("%d", (*grades + i));
        }
    }

}
int Data_duplication(char* temp, char** names, int line)//find if there is another name like this that already entered
{
    for (int i = 0;i < line;i++)
    {
        if (!strcmp(temp, names[i]))
        {
            return 1;
        }
    }
    return 0;
}
int Find(char** names, int* grades, int size, char* name)
{
    int i;
    for (i=0;i < size;i++)
    {
        if (strcmp(name,names[i])==0);
        {
            return (*(grades+i));
        }
    }
    return -1;
}
void FreeAll(char*** names, int** grades, int size)//free al the dynamic memo allocation
{
    for (int i = 0;i < size;i++)
    {
        free(*(*names + i));
    }
    free(*names);
    free(*grades);

}
评论