3
I was doing a program to register students, teachers and subjects in different structs. However I have to link them. For this I made a function only to do this task, but I can not solve a logic error inside.
Follow the line of code that the error is found
strcpy(aluno[alunos[loop2] - 1].materia[aluno[loop2].cont_mate], disc[disciplinas[loop] - 1].nome_disciplina);
strcpy(disc[disciplinas[loop2] - 1].turma_1[disc[loop2].cont_1], aluno[alunos[loop] - 1].nome_aluno);
aluno[loop2].cont_mate++;
disc[loop2].cont_1++;
Exception thrown at 0x0F9CE5A7 (ucrtbased.dll) in Project5.exe: 0xC0000005: Access Violation writing Location 0x0118DC4C.
Function enrollment student(bond):
int matricula_aluno(int qntd_aluno, int qntd_disc, Aluno aluno[], Disciplina disc[])
{
int loop, loop2, qntd_matriculas, qntd_disciplinas;
int alunos[20], disciplinas[20], turma[20];
//deixar todas as posicoes do vetor = 100 para comparacao com o vetor original
for (loop = 0; loop < 20; loop++)
{
alunos[loop] = 100;
disciplinas[loop] = 100;
}
system("cls");
printf("Alunos Cadastrados\n\n");
for (loop = 0; loop < qntd_aluno; loop++)
{
//print de todos os nomes de alunos
printf("%i. %s\n", (loop + 1), aluno[loop].nome_aluno);
}
printf("\nQuantos alunos deseja matricular?\n");
scanf("%i", &qntd_matriculas);
//for de alunos
for (loop = 1; loop < qntd_matriculas + 1; loop++)
{
printf("\nDigite o numero do aluno:\n");
scanf("%i", &alunos[loop - 1]);
}
system("cls");
//____________________________________________________________
printf("Disciplinas Cadastradas\n\n");
for (loop = 0; loop < qntd_disc; loop++)
{
//print de todas as disciplinas
printf("%i. %s\n", (loop + 1), disc[loop].nome_disciplina);
}
printf("\nQuantas disciplinas deseja matricular?\n");
scanf("%i", &qntd_disciplinas);
//for de disciplinas
for (loop = 0; loop < qntd_disciplinas; loop++)
{
printf("\nDigite o numero da disciplina:\n");
scanf("%i", &disciplinas[loop]);
}
for (loop = 0; loop < qntd_disciplinas; loop++)
{
system("cls");
printf("Qual turma deseja para a materia de %s.\n", disc[disciplinas[loop] - 1].nome_disciplina);
printf("1 - Turma 1\n");
printf("2 - Turma 2\n\n");
printf("Turma: ");
scanf("%i", &turma[loop]);
}
for (loop = 0; loop < 5; loop++)
{
for (loop2 = 0; loop2 < 5; loop2++)
{
if (disciplinas[loop] != 100 && alunos[loop2] != 100)
{
//condicao de aplicacao na turma 1 ou na turma 2
if (turma[loop] == 1)
{
if (loop2 < aluno[loop2].cont_mate)
{
strcpy(aluno[alunos[loop2] - 1].materia[aluno[loop2].cont_mate], disc[disciplinas[loop] - 1].nome_disciplina);
strcpy(disc[disciplinas[loop2] - 1].turma_1[disc[loop2].cont_1], aluno[alunos[loop] - 1].nome_aluno);
aluno[loop2].cont_mate++;
disc[loop2].cont_1++;
}
else
{
strcpy(aluno[alunos[loop2] - 1].materia[loop], disc[disciplinas[loop] - 1].nome_disciplina);
strcpy(disc[disciplinas[loop2] - 1].turma_1[disc[loop2].cont_1], aluno[alunos[loop] - 1].nome_aluno);
//copia para a struct o nome das materias que nao tem ´100´ no vetor
aluno[loop2].cont_mate++;
disc[loop2].cont_1++;
}
}
else
{
if (loop2 < aluno[loop2].cont_mate)
{
strcpy(aluno[alunos[loop2] - 1].materia[aluno[loop2].cont_mate], disc[disciplinas[loop] - 1].nome_disciplina);
strcpy(disc[disciplinas[loop2] - 1].turma_2[disc[loop2].cont_2], aluno[alunos[loop] - 1].nome_aluno);
aluno[loop2].cont_mate++;
disc[loop2].cont_2++;
}
else
{
strcpy(aluno[alunos[loop2] - 1].materia[loop], disc[disciplinas[loop] - 1].nome_disciplina);
strcpy(disc[disciplinas[loop2] - 1].turma_2[disc[loop2].cont_2], aluno[alunos[loop] - 1].nome_aluno);
//copia para a struct o nome das materias que nao tem ´100´ no vetor
aluno[loop2].cont_mate++;
disc[loop2].cont_2++;
}
}
}
}
}
return 0;
}
How the structures were defined
Aluno
andDisciplina
? What do you mean by logic error? Gives an error to compile ? Does not do what you want ? Gives only error to execute ?– Isac
The error you are viewing is probably related to an uninitialized string (without the
\0
). This can cause thestrcpy
try writing to an unallocated memory area for the program, which results in the error you are seeing. Just don’t (want to) confirm why it’s really hard to understand what your code is doing. You should use more variables and functions to improve code readability.– Lucas Martins
Managed to solve the problem?
– Mateus -- O Schroeder