Ir para conteúdo
Fórum Script Brasil
  • 0

cadastro de petshop


asantos38

Pergunta

Boa noite pessoal! Fiz um pequeno programa para cadastrar animais clientes de um petshop, mas há alguns erros que não estou conseguindo corrigir.

Vou postar o código fonte e as mensagens de erro.

Não é nenhum trabalho de facul ou trabalho do trabalho(rsrsrs). É apenas um hobby. Mas agradeço quem puder ajudar.


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define limite 4

struct Animal{
    char nome[15];
    int idade;
    int registro;
    char cor[15];
    char raca[15];
    bool vacinado;
};

struct Animal* cria_cadastro(int tamanho);

void limpar_buffer(void);

void cadastrar(struct Animal* cadastro, int tamanho);

struct Animal* buscar(struct Animal* cadastro, int limite);

int main(int argc, char* argv[]){
    struct Animal* cadastro;
    char escolha = '0';
    struct Animal* objeto_da_busca;
    
    cadastro = cria_cadastro(limite);
    
    do {
        printf("[1] - Cadastrar\n");
        printf("[2] - Procurar\n");
        printf("[3] - Encerrar Programa\n\n\n");
        
        printf("Sua escolha? ");
        escolha = getchar();
        
        switch (escolha) {
            case '1':
                cadastrar(cadastro, limite);
                break;
            case '2':
                if ((objeto_da_busca = buscar(cadastro, limite)) != NULL) {
                    printf("Nome: %s\n", objeto_da_busca->nome);
                    printf("Idade: %d\n", objeto_da_busca->idade);
                    printf("Cor: %s\n", objeto_da_busca->cor);
                    printf("Raça: %s\n", objeto_da_busca->raca);
                    printf("Registro: %d\n", objeto_da_busca->registro);
                    
                    if (objeto_da_busca->vacinado == true) {
                        printf("Vacinado: sim\n");
                    }else{
                        printf("Vacinado: não\n");
                    }
                }else{
                    printf("Animal não cadastrado.\n");
                }
                break;
            case '3':
                printf("Encerrando o programa.");
                break;
            default:
                printf("Opção inválida.Tente novamente.\n");
        }
    } while (escolha != '3');
    
    return 0;
}

struct Animal* cria_cadastro(int tamanho){
    struct Animal* cadastro;
    
    cadastro = (struct Animal*)malloc(sizeof(struct Animal) * tamanho);
    
    return cadastro;
}

void limpar_buffer(void){
    char c;
    
    while((c = getchar()) != '\n' && c != EOF){};
}

void cadastrar(struct Animal* cadastro, int tamanho){
    int contador = 0;
    static int rg;
    char continuar;
    
    do {
        system("clear");
        
        printf("Cadastro de Animal\n\n");
        
        printf("Nome: ");
        fgets(cadastro[contador].nome, 15, stdin);
        
        printf("Idade: ");
        scanf("%d", cadastro[contador].idade);
        
        limpar_buffer();
        
        printf("Cor: ");
        fgets(cadastro[contador].cor, 15, stdin);
        
        printf("Raça: ");
        fgets(cadastro[contador].raca, 15, stdin);
        
        cadastro[contador].registro = rg++;
        
        contador++;
    
        printf("\n\n\n\ncadastrar outro animal? ");
        
        continuar = getchar();
        
    } while (contador < tamanho && (continuar != 'n' && continuar != 'N'));

}

struct Animal* buscar(struct Animal* cadastro, int limite){
    char nome_pesquisa[15];
    int contador;
    struct Animal* retorno = NULL;
    
    system("clear");
    
    printf("Busca de Animal\n\n");
    
    printf("Nome Animal: ");
    fgets(nome_pesquisa, 15, stdin);
    
    for (contador = 0; contador < limite; contador++) {
        if (strcmp(cadastro[contador]->nome, nome_pesquisa) == 0) {
            retorno = cadastro[contador];
        }
    }
    
    return retorno;
}

Last login: Thu Sep 12 19:55:56 on console

MacBook-Air-de-Angelo:~ angelo$ cd Documents/C

MacBook-Air-de-Angelo:C angelo$ ls -l

total 0

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 Listas3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 acme

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos3

drwxr-xr-x  5 angelo  staff  160 19 Ago 20:39 arquivos4

drwxr-xr-x  5 angelo  staff  160  8 Set 23:00 arquivos5

drwxr-xr-x  3 angelo  staff   96  8 Set 23:00 arquivos6

drwxr-xr-x  3 angelo  staff   96 12 Set 20:59 arquivos7

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula4

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula5

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 cadastro

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 categoria jogador

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 classifica_pessoa

drwxr-xr-x  4 angelo  staff  128 30 Jul 20:07 cls

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 colisao

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 colisao2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 contador

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 contagem regressiva

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 contagem regressiva2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 conversao

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 conversao2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 cript

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 decisao multipla

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 decisao simples

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 decisao simples2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 dobro de um numero

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 dobro de um numero2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 estrutura

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 fila ex1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 funcao soma

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 funcao soma2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 idade

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 idade_pelo_nascimento

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 idade_pelo_nascimento2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 imc

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 imc2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 imc3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 insercao qualquer

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 inseri_final

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 inserindo_inicio

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 jogos_megasenha

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas ligadas ex1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas ligadas ex2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas ligadas ex3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas ligadas ex4

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas4

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas5

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 listas6

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 loops esccrevendo fatec3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 loops escrevendo FATEC

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 loops escrevendo FATEC2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 maior de dois

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 matrizes

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 matrizes2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 matrizes3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 menu times

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 pilha1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 pilha2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 pilha3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 pilha4

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 pilha5

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 ponteiros_e_vetores_1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 problema das xerox

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 projeto1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 projeto2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 senha_sem_enter

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 soma

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 tabuada de cinco

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 tabuada de numero qualquer

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 teste1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 tipos

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 usando enums

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 usando typedef

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 usando typedef2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 valida_entrada

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 vetor_numeros_nao_repetidos

MacBook-Air-de-Angelo:C angelo$ clear

 

MacBook-Air-de-Angelo:C angelo$ ls -l\more

total 0

drwxr-xr-x  4 angelo  128  8 Set 23:00 vetor_numeros_nao_repetidos

drwxr-xr-x  4 angelo  128  8 Set 23:00 valida_entrada

drwxr-xr-x  4 angelo  128  8 Set 23:00 usando typedef2

drwxr-xr-x  4 angelo  128  8 Set 23:00 usando typedef

drwxr-xr-x  4 angelo  128  8 Set 23:00 usando enums

drwxr-xr-x  4 angelo  128  8 Set 23:00 tipos

drwxr-xr-x  4 angelo  128  8 Set 23:00 teste1

drwxr-xr-x  4 angelo  128  8 Set 23:00 tabuada de numero qualquer

drwxr-xr-x  4 angelo  128  8 Set 23:00 tabuada de cinco

drwxr-xr-x  4 angelo  128  8 Set 23:00 soma

drwxr-xr-x  4 angelo  128  8 Set 23:00 senha_sem_enter

drwxr-xr-x  4 angelo  128  8 Set 23:00 projeto2

drwxr-xr-x  4 angelo  128  8 Set 23:00 projeto1

drwxr-xr-x  4 angelo  128  8 Set 23:00 problema das xerox

drwxr-xr-x  4 angelo  128  8 Set 23:00 ponteiros_e_vetores_1

drwxr-xr-x  4 angelo  128  8 Set 23:00 pilha5

drwxr-xr-x  4 angelo  128  8 Set 23:00 pilha4

drwxr-xr-x  4 angelo  128  8 Set 23:00 pilha3

drwxr-xr-x  4 angelo  128  8 Set 23:00 pilha2

drwxr-xr-x  4 angelo  128  8 Set 23:00 pilha1

drwxr-xr-x  4 angelo  128  8 Set 23:00 menu times

drwxr-xr-x  4 angelo  128  8 Set 23:00 matrizes3

drwxr-xr-x  4 angelo  128  8 Set 23:00 matrizes2

drwxr-xr-x  4 angelo  128  8 Set 23:00 matrizes

drwxr-xr-x  4 angelo  128  8 Set 23:00 maior de dois

drwxr-xr-x  4 angelo  128  8 Set 23:00 loops escrevendo FATEC2

drwxr-xr-x  4 angelo  128  8 Set 23:00 loops escrevendo FATEC

drwxr-xr-x  4 angelo  128  8 Set 23:00 loops esccrevendo fatec3

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas6

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas5

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas4

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas2

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas ligadas ex4

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas ligadas ex3

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas ligadas ex2

drwxr-xr-x  4 angelo  128  8 Set 23:00 listas ligadas ex1

drwxr-xr-x  4 angelo  128  8 Set 23:00 jogos_megasenha

drwxr-xr-x  4 angelo  128  8 Set 23:00 inserindo_inicio

drwxr-xr-x  4 angelo  128  8 Set 23:00 inseri_final

drwxr-xr-x  4 angelo  128  8 Set 23:00 insercao qualquer

drwxr-xr-x  4 angelo  128  8 Set 23:00 imc3

drwxr-xr-x  4 angelo  128  8 Set 23:00 imc2

drwxr-xr-x  4 angelo  128  8 Set 23:00 imc

drwxr-xr-x  4 angelo  128  8 Set 23:00 idade_pelo_nascimento2

drwxr-xr-x  4 angelo  128  8 Set 23:00 idade_pelo_nascimento

drwxr-xr-x  4 angelo  128  8 Set 23:00 idade

drwxr-xr-x  4 angelo  128  8 Set 23:00 funcao soma2

drwxr-xr-x  4 angelo  128  8 Set 23:00 funcao soma

drwxr-xr-x  4 angelo  128  8 Set 23:00 fila ex1

drwxr-xr-x  4 angelo  128  8 Set 23:00 estrutura

drwxr-xr-x  4 angelo  128  8 Set 23:00 dobro de um numero2

drwxr-xr-x  4 angelo  128  8 Set 23:00 dobro de um numero

drwxr-xr-x  4 angelo  128  8 Set 23:00 decisao simples2

drwxr-xr-x  4 angelo  128  8 Set 23:00 decisao simples

drwxr-xr-x  4 angelo  128  8 Set 23:00 decisao multipla

drwxr-xr-x  4 angelo  128  8 Set 23:00 cript

drwxr-xr-x  4 angelo  128  8 Set 23:00 conversao2

drwxr-xr-x  4 angelo  128  8 Set 23:00 conversao

drwxr-xr-x  4 angelo  128  8 Set 23:00 contagem regressiva2

drwxr-xr-x  4 angelo  128  8 Set 23:00 contagem regressiva

drwxr-xr-x  4 angelo  128  8 Set 23:00 contador

drwxr-xr-x  4 angelo  128  8 Set 23:00 colisao2

drwxr-xr-x  4 angelo  128  8 Set 23:00 colisao

drwxr-xr-x  4 angelo  128 30 Jul 20:07 cls

drwxr-xr-x  4 angelo  128  8 Set 23:00 classifica_pessoa

drwxr-xr-x  4 angelo  128  8 Set 23:00 categoria jogador

drwxr-xr-x  4 angelo  128  8 Set 23:00 cadastro

drwxr-xr-x  4 angelo  128  8 Set 23:00 aula5

drwxr-xr-x  4 angelo  128  8 Set 23:00 aula4

drwxr-xr-x  4 angelo  128  8 Set 23:00 aula3

drwxr-xr-x  4 angelo  128  8 Set 23:00 aula2

drwxr-xr-x  4 angelo  128  8 Set 23:00 aula1

drwxr-xr-x  3 angelo   96 12 Set 20:59 arquivos7

drwxr-xr-x  3 angelo   96  8 Set 23:00 arquivos6

drwxr-xr-x  5 angelo  160  8 Set 23:00 arquivos5

drwxr-xr-x  5 angelo  160 19 Ago 20:39 arquivos4

drwxr-xr-x  4 angelo  128  8 Set 23:00 arquivos3

drwxr-xr-x  4 angelo  128  8 Set 23:00 arquivos2

drwxr-xr-x  4 angelo  128  8 Set 23:00 arquivos1

drwxr-xr-x  4 angelo  128  8 Set 23:00 acme

drwxr-xr-x  4 angelo  128  8 Set 23:00 Listas3

MacBook-Air-de-Angelo:C angelo$ clear

 

MacBook-Air-de-Angelo:C angelo$ ls -l | more

total 0

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 Listas3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 acme

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 arquivos3

drwxr-xr-x  5 angelo  staff  160 19 Ago 20:39 arquivos4

drwxr-xr-x  5 angelo  staff  160  8 Set 23:00 arquivos5

drwxr-xr-x  3 angelo  staff   96  8 Set 23:00 arquivos6

drwxr-xr-x  3 angelo  staff   96 12 Set 20:59 arquivos7

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula1

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula3

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula4

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 aula5

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 cadastro

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 categoria jogador

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 classifica_pessoa

drwxr-xr-x  4 angelo  staff  128 30 Jul 20:07 cls

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 colisao

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 colisao2

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 contador

drwxr-xr-x  4 angelo  staff  128  8 Set 23:00 contagem regressiva

MacBook-Air-de-Angelo:C angelo$ cd arquivos7

MacBook-Air-de-Angelo:arquivos7 angelo$ clear

 

MacBook-Air-de-Angelo:arquivos7 angelo$ ls -l

total 8

-rw-r--r--@ 1 angelo  staff  3559 12 Set 20:59 main.c

MacBook-Air-de-Angelo:arquivos7 angelo$ nano main.c

MacBook-Air-de-Angelo:arquivos7 angelo$ gcc -o main main.c

main.c:23:52: error: expected ')'

struct Animal* buscar(struct Animal* cadastro, int limite);

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:23:22: note: to match this '('

struct Animal* buscar(struct Animal* cadastro, int limite);

                     ^

main.c:97:33: error: member reference type 'struct Animal' is not a pointer; did

      you mean to use '.'?

        fgets(cadastro[contador]->nome, 15, stdin);

              ~~~~~~~~~~~~~~~~~~^~

                                .

main.c:100:39: error: member reference type 'struct Animal' is not a pointer;

      did you mean to use '.'?

        scanf("%d", cadastro[contador]->idade);

                    ~~~~~~~~~~~~~~~~~~^~

                                      .

main.c:100:21: warning: format specifies type 'int *' but the argument has type

      'int' [-Wformat]

        scanf("%d", cadastro[contador]->idade);

               ~~   ^~~~~~~~~~~~~~~~~~~~~~~~~

main.c:105:33: error: member reference type 'struct Animal' is not a pointer;

      did you mean to use '.'?

        fgets(cadastro[contador]->cor, 15, stdin);

              ~~~~~~~~~~~~~~~~~~^~

                                .

main.c:108:33: error: member reference type 'struct Animal' is not a pointer;

      did you mean to use '.'?

        fgets(cadastro[contador]->raca, 15, stdin);

              ~~~~~~~~~~~~~~~~~~^~

                                .

main.c:110:27: error: member reference type 'struct Animal' is not a pointer;

      did you mean to use '.'?

        cadastro[contador]->registro = rg++;

        ~~~~~~~~~~~~~~~~~~^~

                          .

main.c:110:38: error: expression is not assignable

        cadastro[contador]->registro = rg++;

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^

main.c:122:52: error: expected ')'

struct Animal* buscar(struct Animal* cadastro, int limite){

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:122:22: note: to match this '('

struct Animal* buscar(struct Animal* cadastro, int limite){

                     ^

main.c:122:52: error: parameter name omitted

struct Animal* buscar(struct Animal* cadastro, int limite){

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:135:38: error: member reference type 'struct Animal' is not a pointer;

      did you mean to use '.'?

        if (strcmp(cadastro[contador]->nome, nome_pesquisa) == 0) {

                   ~~~~~~~~~~~~~~~~~~^~

                                     .

main.c:136:21: error: assigning to 'struct Animal *' from incompatible type

      'struct Animal'; take the address with &

            retorno = cadastro[contador];

                    ^ ~~~~~~~~~~~~~~~~~~

                      &

1 warning and 11 errors generated.

MacBook-Air-de-Angelo:arquivos7 angelo$ clear

 

MacBook-Air-de-Angelo:arquivos7 angelo$ gcc -o main main.c

main.c:23:52: error: expected ')'

struct Animal* buscar(struct Animal* cadastro, int limite);

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:23:22: note: to match this '('

struct Animal* buscar(struct Animal* cadastro, int limite);

                     ^

main.c:100:21: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]

        scanf("%d", cadastro[contador].idade);

               ~~   ^~~~~~~~~~~~~~~~~~~~~~~~

main.c:122:52: error: expected ')'

struct Animal* buscar(struct Animal* cadastro, int limite){

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:122:22: note: to match this '('

struct Animal* buscar(struct Animal* cadastro, int limite){

                     ^

main.c:122:52: error: parameter name omitted

struct Animal* buscar(struct Animal* cadastro, int limite){

                                                   ^

main.c:6:16: note: expanded from macro 'limite'

#define limite 4

               ^

main.c:135:38: error: member reference type 'struct Animal' is not a pointer; did you mean to use

      '.'?

        if (strcmp(cadastro[contador]->nome, nome_pesquisa) == 0) {

                   ~~~~~~~~~~~~~~~~~~^~

                                     .

main.c:136:21: error: assigning to 'struct Animal *' from incompatible type 'struct Animal'; take

      the address with &

            retorno = cadastro[contador];

                    ^ ~~~~~~~~~~~~~~~~~~

                      &

1 warning and 5 errors generated.

MacBook-Air-de-Angelo:arquivos7 angelo$

 

 

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

1) Contantes definidas com o #define devem ter nome único. Na função buscar você utilizou o nome "limite" para uma variável.
2) Para facilitar na nomenclatura, use o typedef para renomear sua estrutura.
3) +alguns erros de operadores com ponteiros:

if (strcmp(cadastro[contador]->nome, nome_pesquisa) == 0) {
            retorno = cadastro[contador];

  Mudar para:

 if (strcmp(cadastro[contador].nome, nome_pesquisa) == 0) {
            *retorno = cadastro[contador];


Eu consertei seu código e rodei ele. Vi que possui alguns bugs. Continue o seu trabalho 🙂
 

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

#define LIMITE 4

typedef struct Animal{
    char nome[15];
    int idade;
    int registro;
    char cor[15];
    char raca[15];
    bool vacinado;
} Animal;

Animal* cria_cadastro(int tamanho);

void limpar_buffer(void);

void cadastrar(Animal* cadastro, int tamanho);

Animal* buscar(Animal* cadastro, int lim);

int main(int argc, char* argv[]){
    Animal* cadastro;
    char escolha = '0';
    Animal* objeto_da_busca;
    
    cadastro = cria_cadastro(LIMITE);
    
    do {
        printf("[1] - Cadastrar\n");
        printf("[2] - Procurar\n");
        printf("[3] - Encerrar Programa\n\n\n");
        
        printf("Sua escolha? ");
        escolha = getchar();
        
        switch (escolha) {
            case '1':
                cadastrar(cadastro, LIMITE);
                break;
            case '2':
                if ((objeto_da_busca = buscar(cadastro, LIMITE)) != NULL) {
                    printf("Nome: %s\n", objeto_da_busca->nome);
                    printf("Idade: %d\n", objeto_da_busca->idade);
                    printf("Cor: %s\n", objeto_da_busca->cor);
                    printf("Raça: %s\n", objeto_da_busca->raca);
                    printf("Registro: %d\n", objeto_da_busca->registro);
                    
                    if (objeto_da_busca->vacinado == true) {
                        printf("Vacinado: sim\n");
                    }else{
                        printf("Vacinado: não\n");
                    }
                }else{
                    printf("Animal não cadastrado.\n");
                }
                break;
            case '3':
                printf("Encerrando o programa.");
                break;
            default:
                printf("Opção inválida.Tente novamente.\n");
        }
    } while (escolha != '3');
    
    return 0;
}

Animal* cria_cadastro(int tamanho){
    Animal* cadastro;
    
    cadastro = (Animal*)malloc(sizeof(Animal) * tamanho);
    
    return cadastro;
}

void limpar_buffer(void){
    char c;
    
    while((c = getchar()) != '\n' && c != EOF){};
}

void cadastrar(Animal* cadastro, int tamanho){
    int contador = 0;
    static int rg;
    char continuar;
    
    do {
        system("clear");
        
        printf("Cadastro de Animal\n\n");
        
        printf("Nome: ");
        fgets(cadastro[contador].nome, 15, stdin);
        
        printf("Idade: ");
        scanf("%d", &(cadastro[contador].idade));
        
        limpar_buffer();
        
        printf("Cor: ");
        fgets(cadastro[contador].cor, 15, stdin);
        
        printf("Raça: ");
        fgets(cadastro[contador].raca, 15, stdin);
        
        cadastro[contador].registro = rg++;
        
        contador++;
    
        printf("\n\n\n\ncadastrar outro animal? ");
        
        continuar = getchar();
        
    } while (contador < tamanho && (continuar != 'n' && continuar != 'N'));

}

Animal* buscar(Animal* cadastro, int lim){
    char nome_pesquisa[15];
    int contador;
    Animal* retorno = NULL;
    
    system("clear");
    
    printf("Busca de Animal\n\n");
    
    printf("Nome Animal: ");
    fgets(nome_pesquisa, 15, stdin);
    
    for (contador = 0; contador < lim; contador++) {
        if (strcmp(cadastro[contador].nome, nome_pesquisa) == 0) {
            *retorno = cadastro[contador];
        }
    }
    
    return retorno;
}

 

Editado por Hazzu
Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...