removing a student from an array of struct

Advertisements

I’m working on a project and for that I need a function for removing a student by searching id but when I run it and enter a valid id to remove a student it doesn’t work and it goes to else block and print that the id us invalid.

void remove_student()
{
    char removing_id[12];

    printf("\nPlease enter the id of student that you want to remove : ");
    scanf("%11s", removing_id);

    for (int i = 0; i < students_count; i ++)
    {
        if (strcmp(removing_id, student[i].id) == 0)
        {
            for (int j = i; j < students_count; j++)
            {
                strcpy(student[i].first_name, student[i+1].first_name);
                strcpy(student[i].last_name, student[i+1].last_name);
                strcpy(student[i].id, student[i+1].id);
                student[i].average = student[i+1].average;
            }
            students_count --;

            printf("The student removed successfully.");
        } else
        {
            printf("The id is not available!");
            exit(0);
        }
    }
}

Then main problem with this function is that when for example I have 3 students and I want to remove the second one with id it prints"The student removed successfully." and "The id is not available!" too.
but doesn’t remove the student.

>Solution :

"The student removed successfully." and "The id is not available!" too

This is because you are not breaking the loop once you find the student in the if case.

need a function for removing a student by searching id but when I run it and enter a valid id to remove a student it doesn’t work and it goes to else block and print that the id us invalid.

There is an logical error in your code. If the student you want to remove is not in the first index the code will goto else case and stops the program.

I think the below code is what you may want:

void remove_student()
{
    char removing_id[12];
    int i;

    printf("\nPlease enter the id of student that you want to remove : ");
    scanf("%11s", removing_id);

    for (i = 0; i < students_count; i++)
    {
        if (strcmp(removing_id, student[i].id) == 0)
        {
// students_count - 1 to handle array out of bounds
            for (int j = i; j < students_count - 1; j++)
            {
// I think here it should be j, j+1 if you are trying to move student data
                strcpy(student[j].first_name, student[j+1].first_name);
                strcpy(student[j].last_name, student[j+1].last_name);
                strcpy(student[j].id, student[j+1].id);
                student[j].average = student[j+1].average;
            }
            break;
        }
  }

  if(i == students_count) // Loop ran to completion hence no student found.
  {
        printf("The id is not available!");
        exit(0); // Do you really need to exit the program? may be returning some bool is a good idea
  }
  else // Loop stopped in between hence student found.
  {
        student_count--;
        printf("The student removed successfully.");
  }
}

Leave a ReplyCancel reply