error when registering a subject on a student’s list

Asked

Viewed 48 times

-2

I’m doing a school register where I create a double-chained circular list to insert students, and a simply chained list to insert a student’s discipline. In the part of enrolling student everything went well, but when entering the discipline list in the list of selected student of error. Anyone who can help me, thank you. Follow the code:

pupil. h

#ifndef ALUNO_H_INCLUDED
#define ALUNO_H_INCLUDED

typedef struct DisciplinasListas {
    char nome[100];
    struct DisciplinasListas * prox;
}Disciplina;

struct AlunosLista{
    char nome[100], dataNascimento[11],matricula[10];
    Disciplina * disciplinas;
    struct AlunosLista * next;
    struct AlunosLista * prev;
};

void cadastrarAluno();
void imprimirLista();
void cadastrarDisciplinas();
struct AlunosLista * buscaAluno(char *);

#endif

main. c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "aluno.h"

typedef struct AlunosLista node;
node *head = NULL, *tail = NULL;

void cadastrarAluno() {
    node *newNode = (node *) malloc(sizeof(node));
    printf("\nForneca a matricula: ");
    fgets(newNode->matricula,10,stdin);
    setbuf(stdin,NULL);
    printf("\nForneca o nome: ");
    fgets(newNode->nome,100,stdin);
    setbuf(stdin,NULL);
    printf("\nForneca a data de nascimento: ");
    fgets(newNode->dataNascimento,11,stdin);
    setbuf(stdin,NULL);
    newNode->disciplinas=NULL;

    newNode->next = newNode;
    newNode->prev = newNode;

    if(head==NULL) {
        head = newNode;
        tail = newNode;
    } else {
        tail->next = newNode;
        newNode->next = head;
        newNode->prev = tail;
        tail = newNode;
        head->prev = tail;
    }
}

void imprimirLista() {
    if(head==NULL)  return;
    node *current = head;
    do {
        printf("\n%s", current->matricula);
        Disciplina *currentDisc = current->disciplinas;
        do {
            printf("\n%s", currentDisc->nome);
            currentDisc = currentDisc->prox;
        } while(currentDisc != current->disciplinas);
        current = current->next;
    }   while(current != head);
}

struct AlunosLista * buscaAluno(char matricula[]) {
    if(head==NULL)  return;
    node *current = head;
    do {
        if(!strcmp(current->matricula,matricula)) {
            return current;
            current = current->next;
        }
    }   while(current != head);
    return 0;
}

void cadastrarDisciplinas() {
    char disciplina[100], matricula[10];
    if(head==NULL)  return;

    printf("\nForneça a matricula do aluno:");
    fgets(matricula,10,stdin);
    setbuf(stdin,NULL);
    struct AlunosLista *busca = buscaAluno(matricula);
    if(busca!=0) {
        printf("\nForneça o nome da disciplina:");
        fgets(disciplina,100,stdin);
        setbuf(stdin,NULL);
        node *current = head;
        do {
            if(current==busca) {
                if (!strcmp(current->matricula,matricula)) {
                    Disciplina * lDisciplina = (Disciplina *)malloc(sizeof(Disciplina));
                    strcpy(lDisciplina->nome,disciplina);
                    lDisciplina->prox = current->disciplinas;
                    current->disciplinas->prox = lDisciplina;
                    break;
                }
            }
            current = current->next;
        }   while(current != head);

    } else
        printf("\nAluno inexistente!!!");
}

int main(){
    int opcao;
    do{
        printf("\n1-cadastrar aluno");
        printf("\n2-cadastrar disciplinas do aluno");
        printf("\n3-listar");
        printf("\n4-sair");
        printf("\nForneca a sua opcao: ");
        scanf("%d",&opcao);
        setbuf(stdin,NULL);
        switch(opcao){
            case 1:
                cadastrarAluno();
                break;
            case 2:
                cadastrarDisciplinas();
                break;
            case 3:
                imprimirLista();
                break;
        }
    }while(opcao!=4);
    return 0;
}
  • In his function cadastrarDisciplinas the function buscaAluno no longer returns the pointer to the student? Why do you go back through the list of students? I think you should use Disciplina * disciplinas of the structure Alunoslista to register the disciplines. You should not check any duplicities of disciplines?

1 answer

0

Entering only on the merit of the problem you are facing to insert the values of disciplines for a student, you are trying to insert a value that depends on memory allocation without calling the function malloc before:

        Disciplina * lDisciplina = (Disciplina *)malloc(sizeof(Disciplina));
        strcpy(lDisciplina->nome,disciplina);
        lDisciplina->prox = current->disciplinas;
        current->disciplinas->prox = lDisciplina; // "prox", por exemplo, é um ponteiro

Before inserting the values in the pointers, it is necessary to reserve a memory region for them, for example:

        Disciplina * lDisciplina = (Disciplina *)malloc(sizeof(Disciplina));
        strcpy(lDisciplina->nome,disciplina);

        // pode ser que o aluno atual não tenha disciplinas ainda; 
        // verifique aqui se o aluno ja possui uma lista de disciplinas existente antes de reservar memória
        current->disciplinas = malloc(sizeof(Disciplina));
        lDisciplina->prox = current->disciplinas;
                
        current->disciplinas->prox = malloc(sizeof(Disciplina));
        current->disciplinas->prox = lDisciplina;

Browser other questions tagged

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