Program in C where you have to remove a student from a vector

Asked

Viewed 904 times

0

I have to make a college program that remove a student from a vector, that when I run the code below, it excludes the student but leaves a special character in the code, can 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();
 }
  • Well, your code seems to be removing ALL students when the typed code is found among existing students. You make two loops, at first search for the code and mark if found. In the second loop (which is unnecessary, since you could have removed it when you found it, in the first loop) you simply check whether you found and remove IT in EACH LOOP INTERACTION. It just doesn’t seem right anymore...

  • But, anyway, what does it mean to "leave a special character in the code"? In what code? What do you mean?

  • 1

    In addition, the p[i+1] is accessing an element after the end of the array. In C this gives unpredictable / results (maybe the weird character is due to this)

  • 1

    @hugomg "gives shit" is the best technical term Ever! :)

1 answer

1

As said by @Luiz Vieira in the comments of the question, the two loops could be merged. For examples where it reads encontrou == 1 in the second loop, could be exchanged for (strcmp(codigo,p[i].codigo))== 0, avoiding bankrupting the school and removing all students.

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

What I advise you to do is to reuse the loop, being careful when you reach the limit of your list. Note also that I have not removed the part where p[i] = p[i+1], because I don’t 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 of codigo (which is already itself a char pointer. As printf accepts only one pointer, Voce could pass only: printf("Codigo Excluido: %s \n", codigo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.