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...
– Luiz Vieira
But, anyway, what does it mean to "leave a special character in the code"? In what code? What do you mean?
– Luiz Vieira
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)– hugomg
@hugomg "gives shit" is the best technical term Ever! :)
– Luiz Vieira