Create exclude function in C project

Asked

Viewed 1,283 times

-1

I have created a school library project. I have no idea to create delete function:

#include<stdio.h>
#define MAX 15

typedef struct TAutor {
    char Nome[40];
    char   Pais[20];
    char cidade[30];
    char isbn[25];
    int idade;
}Autor;

typedef struct Tlivro {
    char Nome_livro[20];
    Autor Nome_autor[20];
    char pais_local[30];
    char cidade_local[30];
    char isbn[25];
    int ano_lancamento;
}Livro;

 void ver_vazio(Livro cadas_livro[MAX]);
    void inserir();
    void listar();
    void eliminar();

main()
{   int opc=0,cont=0,fim=0,men=1,sair=0,pin=0,i=0;

    Livro cadas_livro[MAX];
    printf("\t########################################################\n");
    printf("\t###       Gestao da biblioteca                 ###\n");
    printf("\t########################################################\n");
    printf("\t###        [1]->Inserir Dados no Sitema              ###\n");
    printf("\t###        [2]->Ver Dados no Sistema               ###\n");
    printf("\t###                                                  ###\n");
    printf("\t###                                                  ###\n");
    printf("\t###                                                  ###\n");
    printf("\t########################################################\n");
    printf(" \n");

do{
    printf("\tEscolha a opcao:");
    scanf("%d",&opc);
    fflush(stdin);

            switch(opc)
            {
                case 1:
                    do
                    {

                                  inserir();
                                system("cls");
                                 //este codi foi feito para aparecer apenas a pergunta será sempre falço se escolher esta opções
                                 printf("\n");
                                 puts("\tse pretende continuar[1] se nao[2]");
                                 scanf("%d",&fim);

                    }while(men==fim);break;
                       //inserir();break;
                          //  men=getch();
                            //while(men=getch())


                case 2:
                    printf("\n");
                listar();break;
                default :printf("Esta opcao nao existe");
            }
    printf("\n");
    printf("\tOpcao do menu Continuar[1]-Sair[0]");
    scanf("%d",&sair);
}while(sair==1);

}
//função inserir
void inserir()

{   Livro cadas_livro[MAX];
    int i=0;
    fflush(stdin);
    printf("\tInsira o nome do livro: ");
    fgets(cadas_livro[i].Nome_livro,20,stdin);
    printf("\tInsira o nome do autor: ");
    fgets(cadas_livro[i].Nome_autor[i].Nome,40,stdin);
    printf("\tPais de lancamento: ");
    fgets(cadas_livro[i].pais_local,30,stdin);
    printf("\tCidade de Lancamento: ");
    fgets(cadas_livro[i].cidade_local,30,stdin);
    printf("\tISbn:");
    fgets(cadas_livro[i].isbn,25,stdin);
    i++;

}

void listar(int dado)
{   fflush(stdin);
    Livro cadas_livro[MAX];
    int i=0;

        printf("\tNome do livro:%s ",cadas_livro[i].Nome_livro);
    printf("\tNome do autor:%s ",cadas_livro[i].Nome_autor[i].Nome);
    printf("\tCidade de Lancamento %s",cadas_livro[i].cidade_local);
    printf("\tPais de lancamento:%s ",cadas_livro[i].pais_local);
    printf("\tISbn:%s ",cadas_livro[i].isbn);



    printf(" \n");
    printf("\t\t\t\tSoftware Fabricado Pela Erms.system \n");
    printf("\t\t\t\t###################################\n");
}

void eliminar()
{ int i;
        Livro cadas_livro[MAX],Nome_livro;
        printf("Insira o nome do livro a eliminar");
        fgets(cadas_livro[i].Nome_livro,20,stdin);
        for(i=0;i<=MAX;i++)
        {
            if(cadas_livro[i]==Nome_livro)
            cadas_livro[i].isbn=cadas_livro[i+1];
            else
            printf("Livro nao encontrado\n");
        }
}
  • 2

    welcome to Stackoverflow, you can access this link, How to ask a good question, to find out how to organize your question, then click on Edit to make the necessary changes.

  • 1

    You are using a vector of books, which is not dynamic. What do you imagine to represent a deleted book? A deleted field? Reset the position information?

  • In fact I intend to create a function that gives the possibility to delete a book stored in the vector

  • I just want to create a delete function

  • Did the posted answer solve your problem? Do you think you can accept it? If you don’t know how to do it, check out [tour]. This would help a lot to indicate that the solution presented was useful to you and to give an indication that it is satisfactory. You can also vote on any and all questions or answers you find useful on the entire site (when you have 15 points). Accepting and voting are separate things.

1 answer

2

This code has several other errors, so I didn’t even try to test. That answers the question, but it doesn’t make the code work. It’s kind of like this here:

void eliminar() { //o certo é receber o array como parâmetro
    Livro cadas_livro[MAX]; //isto está bem errado, em todas funções
    char Nome_livro[20];
    printf("Insira o nome do livro a eliminar");
    fgets(Nome_livro, 20, stdin);
    for (int i = 0; i <= MAX; i++) { //MAX não vai dar certo se o número é variável
        if (strcmp(cadas_livro[i].Nome_livro, Nome_livro)) {
            for (int j = i; j <= MAX; j++) { //precisa "puxar" todos os seguintes
                cadas_livro[j] = cadas_livro[j + 1];
            }
        } else printf("Livro nao encontrado\n");
    }
}

I put in the Github for future reference.

Browser other questions tagged

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