Doubt when creating structs, relating one struct to another

Asked

Viewed 78 times

0

I created the following structs:

typedef struct cliente{
    unsigned long int codigo;
    char nome[50];
    char endereco[50];
    char cpf[11];
    char idade[3];
    struct cliente* prox;
}Cliente;

typedef struct funcionario{
    unsigned int codigo;
    char nome[50];
    char endereco[50];
    struct funcionario* prox;
}Funcionario;

typedef struct veiculo {
    unsigned int codigo;
    char placa[6];
    char modelo[50];
    char cor[30];
    char assentos[2];
    struct veiculo* prox;
}Veiculo;

They have the function of storing data and registering some information about client, vehicle and official, thus: (the others have the same structure):

//Inserindo clientes
Cliente* inserir_cliente(Cliente *primeiroCliente){
    Cliente cliente;
    Cliente *atual = primeiroCliente;
    char identificador = 'F' ;

    //Lendo as informacoes do cliente.
    printf(" Codigo do cliente: ");
    scanf("%u", &cliente.codigo); printf("\n");
    printf(" Nome: ");
    fflush(stdin); fgets(cliente.nome, 40, stdin); printf("\n");
    printf(" Endere%co: ", 135);
    fflush(stdin); fgets(cliente.endereco, 40, stdin); printf("\n");
    printf(" CPF: ");
    fflush(stdin); fgets(cliente.cpf, 11, stdin); printf("\n");
    printf(" Idade: ");
    fflush(stdin); fgets(cliente.idade, 11, stdin); printf("\n");

    //Verificando se o cadastro ja existe.
    for(atual=primeiroCliente; atual!=NULL; atual=atual->prox){
        if(atual->codigo==cliente.codigo){
            identificador = 'V';
            break;
        }
    }

    if(identificador!='V' && (strlen(cliente.nome)!=1 && strlen(cliente.endereco)!=1)){
        //Alocando espacos e guardando informacoes do cliente.
        Cliente* NovoCliente=(Cliente*) malloc(sizeof(Cliente));
        strcpy(NovoCliente->nome, cliente.nome);
        strcpy(NovoCliente->endereco, cliente.endereco);
        strcpy(NovoCliente->cpf, cliente.cpf);
        strcpy(NovoCliente->idade, cliente.idade);
        NovoCliente->codigo = cliente.codigo;
        NovoCliente->prox = primeiroCliente;
        printf(" Cadastro realizado com sucesso.");
        printf("\n\n PRESSIONE QUALQUER TECLA PARAVA VOLTAR AO MENU PRINCIPAL.");
        return NovoCliente;
    }else{
        printf(" Cadastro inv%clido.", 160);
        printf(" \n\n PRESSIONE QUALQUER TECLA PARA VOLTAR AO MENU PRINCIPAL.");
        return primeiroCliente;
    }

}

My doubt is the following, I also made the following structure:

typedef struct locacao {
    unsigned long int codigo;
    int cod_veiculo;
    char modelo[50];
    char cor[30];
    int codCliente;
    char nomeCliente[50];
    char idadeCliente[2];
    int codFuncionario;
    char nomeFuncionario[50];
    Data locacao;
    struct locacao* prox;
}Locacao;

I wondered if there was some way I could input information into the struct Leasing from the data already registered in the struct, client, staff and vehicle.

Note: I’m doing it in Devc++, in windows environment.

Who wants to check the whole code: https://drive.google.com/open?id=1Q5Bl5Tf0F0sIiz_lWFmtn0DKS-whZ1NZ

  • Just from the perspective of helping with this and future issues, the Devc++ tag you have posed does not apply to this question, nor to any of the ones you have asked behind because it has no influence on the code itself. It would only make sense to put if you were using any specific editor functionality. You can however indicate as an observation, as you did, to give more context to those who read the question, if you think it is relevant.

  • On what part of struct locacao would the customer for example ? No int codCliente; ?

  • Thank you very much for the suggestion. That’s what I’d like to insert the int codCliente the system does a search on struct cliente and return char nomeCliente[50], and the char IdadeCliente[2].

  • Make a search based on id is different from each locacao having a pointer to the corresponding client, the second option being generally preferable as it reduces redundancy.

No answers

Browser other questions tagged

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