C program where you have to remove a student from a vector

0

I have to make a program that college that remove a student from a vector, that when I run the code below, it deletes the student plus leaves a special character in the code, can you help me?

//Remover o Aluno
void removerAluno(DADOSPES p[], int n)
 {
     char codigo[10];
     int i;
     printf("Digite o Codigo que deseja excluir: ");
     fflush(stdin);
     gets(codigo);
     int encontrou =0;
     for(i=0; i<n; i++)
     {
         if((strcmp(codigo,p[i].codigo))== 0)
         {
            encontrou =1;
         }   
     }
     for(i=0; i<n; i++)
     {
        if(encontrou == 1)
          {
            system("cls");
            p[i] = p[i+1];
            p[i].DiaN = p[i+1].DiaN;
            p[n-1].DiaN = 0;
            p[i].MesN = p[i+1].MesN;
            p[n-1].MesN = 0;
            p[i].AnoN = p[i+1].AnoN;
            p[n-1].AnoN = 0;
            p[i].idade = p[i+1].idade;
            p[n-1].idade = 0;
          }
     } 
     printf("Codigo Excluido: %s \n",&codigo );
     system("pause");
     getch();
 }
    
asked by anonymous 27.04.2015 / 02:34

1 answer

1

As said by @Luiz Vieira in the comments of the question, the two ties could be merged. For example, where%% of the second loop is read, it could be exchanged for encontrou == 1 , avoiding bankruptcy and removing all students.

Another problem is that you apparently want to fill in the space left by the student to be removed. As said by the @hugomg companion in the comments, using (strcmp(codigo,p[i].codigo))== 0 you end up using a pointer out of your table when p[i+1] , which can cause random errors running.

What I advise you to do is reuse the loop, being careful when you reach the edge of your list. Note also that I did not remove the part where i==9 , because I do not know the structure.

for(i=0;i<n; i++)
{
    if(strcmp(codigo, p[i].codigo) == 0)
    {
        encontrou = 1;
    }
    if(encontrou == 1 && i < n-1)
    {
        p[i].DiaN = p[i+1].DiaN;
        p[i].MesN = p[i+1].MesN;
        p[i].AnoN = p[i+1].AnoN;
        p[i].idade = p[i+1].idade;
        p[i].codigo = p[i+1].codigo; //???
   }
}
if(encontrou == 1)
{
    p[n-1].MesN = 0;
    p[n-1].DiaN = 0;
    p[n-1].AnoN = 0;
    p[n-1].idade = 0;
}

However, to answer your question about the special character at the end, it may be because you are passing a pointer to p[i] = p[i+1] (which is already a char pointer in itself.) printf accepts only one pointer, you could only pass: codigo

    
29.04.2015 / 01:51