2

正规365体育投注Input data works for me in the Init function. But once I put in some names and ask for the score of an existing name or a non-existent name, it always returns the grade of the first name

I create two arrays. One of names and one of grade. Checks whether they meet all the terms of "proper name" as defined by us. Takes another name and checks if it is in a database. If so, returns the score if it does not return -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);

}

enter image description here

| improve this question | | | | |
New contributor
בר ס is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 3
    🚫📸 Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the {} button to mark blocks of code, or indent with four spaces for the same effect. The contents of a screenshot can’t be searched, run as code, or copied and edited to create a solution. – tadman yesterday
  • 🔎🐞Did run your code in a debugger to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman yesterday
  • Seeing char*** as an argument type is not a great sign. That's a lot of pointer indirection! – tadman yesterday
  • That's what we were asked to do. What should I fix? Did you see the problem I was talking about? – בר ס yesterday
  • 1
    i found the problam...a rookie mistake...in the "Find" function i put ; after the if...the program works. Anyway, thanks for the help :) – בר ס yesterday

Your Answer

בר ס is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy正规365体育投注

Browse other questions tagged or ask your own question.