How do I store data in a structure and then use it in different functions?

Asked

Viewed 92 times

1

Guys I’m trying to schedule this work for days and I can’t get out of the corner, it’s a library system that in turn needs:

  • a) Register, view, change and remove data from a book;
  • b) Register, view, change and remove new customers;
  • c) Make a new loan;
  • d) Make a return;
  • e) List all books lent to a client;

My biggest problem is to store the data and use it again, I create the pointer to a struct and when I try to use the data saved in it I can’t follow what you can do until now

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct livro{
    char titulo[81];
    char autor[41];
    char editora[31];
    int ano;
    int codigo;
};

typedef struct cliente{
    char *nome[50];
    char *cpf[11];
    char *matricula[6];     
}cliente;

int main(){
    FILE *file; 
void MenuSecundario();
void VisualizarDados();
    int opcao, menu, i;
    do
    {
    printf("\t\t\nSelecione uma das opcoes: \n");
    printf("1. Cadastrar, altera ou remover dados de cliente: \n");
    printf("2. Cadastrar, altera ou remover livro : \n");
    printf("3. Visualizar dados de clientes: \n");
    printf("4. Visualizar dados de livros: \n");
    printf("5. Realizar novo emprestimo: \n");
    printf("6. Realizar devolucao: \n");
    printf("0. Sair\n");
    printf("Opcao: ");
    scanf("%d", &opcao);

    switch( opcao ){

    case 0:
    system("cls || clear");
    printf("Fechando progama...\n");
    break;
    case 1:
    system("cls || clear");
    cliente kirito;
    MenuSecundario(&kirito);
    break;
    case 2:
    system("cls || clear");

    break;
    case 3:
    VisualizarDados();
    system("cls || clear");

    break;
    case 4:
    system("cls || clear");

    break;
    case 5:
    system("cls || clear");

    break;
    case 6:
    system("cls || clear");

    break;  
    default:
    system("cls || clear");
    printf("Opcao invalida! Tente novamente.\n");
}
    } while(opcao);

    return 0;
}

void MenuSecundario(cliente *kirito[50]){
    int menu,i,aux=1;
    int pos=0;
    do{
    system("cls || clear");
    printf("Selecione uma das opcoes: \n");
    printf("1. Cadastrar novo cliente: \n");
    printf("2. Alterar dados de clientes: \n");
    printf("3. Remover cliente: \n");
    printf("0. Sair\n");
    printf("Opcao: ");
    scanf("%d", &menu);


    switch(menu){

    case 0:
    system("cls || clear");
    printf("Saindo do menu...\n");
    break;
    case 1:
    system("cls || clear");
    printf("\n--------------------------Cadastrar novo cliente--------------------------\n");
    printf("\n\n");
    for(i=0; i<50; i++){
        while(aux==1){
    printf("Digite o nome do cliente: \n");
    scanf(" %[^\n]s", &kirito[i]->nome);
    printf("Digite o cpf do cliente: \n");
    scanf(" %[^\n]s", &kirito[i]->cpf);
    printf("Digite a matricula do cliente: \n");
    scanf(" %[^\n]s", &kirito[i]->matricula);
    system("cls || clear");
    printf("Deseja cadastrar novo usuario ?\n");
    printf("1. sim.\n");
    printf("2. nao.\n");
    printf("opcao:");
    scanf("%d", &aux);
    system("cls || clear");
    }
}
break;

    case 2:
    system("cls || clear");

    break;

    case 3:
    system("cls || clear");

    break;
    default:
    system("cls || clear");
    printf("Opcao invalida! Tente novamente.\n");
  }
 }while(menu);
}

void VisualizarDados(cliente *kirito){
    int i;
    for(i=0; i<50; i++){
    printf("%s", kirito->nome);
}
}

1 answer

0

Since you used a sequential list, in the function that displays the information you also need to use it.

void VisualizarDados(cliente *kirito) { 
    int i; 
    for(i=0; i<50; i++) { 
        printf("%s", kirito[i]->nome); //alteração nessa linha
    }
}

I recommend you use a more dynamic list. This one is a good example for you to start with chain list.

  • the problem that when I make this change in the line, it doesn’t print anything, as if the struct had empty you know ? I put to print any message, she printed this message more not the data that should be in the struct.

  • Try to pass the struct you want to view as a parameter to the function VisualizarDados(&kirito); and also specify the position you want to display kirito[i]->nome

Browser other questions tagged

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