Registration using struct

Asked

Viewed 485 times

-1

I need to create a registration algorithm that registers, displays and deletes. I could not in the delete part. some gives me a light plsss!!!

#include <stdlib.h>
#include<stdio.h>
#include <conio.h>
#include<math.h>
#include<locale.h>
#include<string.h>


int i=0,cdg, cont=0;

typedef struct {
                char nome[30];
                char rua [50];
                int numero;      
                }cadastro;


cadastro cadastrar()
{    
   cadastro c;
    printf ("\n Sistema Cadastrar Clientes ");
    printf ("\n Informe o nome:  ");
    fflush(stdin);
    scanf("%s", c.nome);
    printf (" Informe a rua:     ");
    fflush(stdin);
    scanf("%s",c.rua);
    printf (" Informe o numero:  ");
    scanf("%d",&c.numero);
    cont++;

    return c;
}

 exibir (cadastro cad[])
{ 
  printf("\n Exibição de cadastro    \n");
    for (int j=0;j<cont;j++){
          printf("\n     CODIGO:   %d", i);
          printf("\nNome ...........: %s", cad[j].nome);
          printf("\nRua ...........: %s", cad[j].rua);
          printf("\nNumero ...........: %d\n", cad[j].numero);

    }
        system ("pause >>NULL");
}


 excluir(cadastro cad[])
{   int opc,i,j;
    printf ("Informe o codigo que deseja excluir: ");
    scanf("%d",&opc);
    for (i=0;i=opc;i++){
        for (j=0;j=!'/0';j++){
           cadastro cad [j].nome = "";
           cad[j].rua = ' ';
           cad[j].numero = 0;
    }
}

}


int main(){
    setlocale(LC_ALL,"portuguese")
    system("cls");
      int opc;
      cadastro cad[500];



    do{
    system("cls");
    printf ("\n SISTEMA CADASTRO \n\n 1 - CADASTRAR\n 2 - EXIBIR      **** 5 p/ SAIR");
    printf ("\n\n Digite a opção:     ");
    scanf ("%d", &opc);
    system("cls");
   switch(opc){
    case 1 : cad [i]=cadastrar();
             i++;
             break;
    case 2 : exibir(cad);
             break; 
    case 3: excluir (cad)         
            break; 
   printf ("\n SISTEMA CADASTRO \n\n 1 - CADASTRAR\n 2 - EXIBIR      **** 5 p/ SAIR");
}while(opc != 5);
}

1 answer

0

Deletion is done by decreasing the size you are currently considering, in your case given by the variable cont, and pulling all elements from the removed one home back, thus leaving the last house "empty".

Visually something like this:

[a, b, c, d, e, f] - Tamanho 6

Removing the c:

[a, b, d, e, f,  ] - Tamanho 5

What you do is start at c, and put the d in the c, then put the e in the d, and so on. In the end you can choose to clean the last one or not, but typically it will make no difference depending on your insertion algorithm.

Code C adjusted to your program:

void excluir(cadastro cad[])
{   
    int opc,i;
    printf ("Informe o codigo que deseja excluir: ");
    scanf("%d",&opc);
    for (i=opc;i < cont - 1;i++){ //começa no a remover e vai até ao penúltimo
        cad[i] = cad[i + 1]; //coloca o da frente no corrente
    }

    cont--; //diminuir o tamanho que está a ser considerado
}

Note that I chose not to "clean" the last record because it won’t make a difference unless your algorithm does something it shouldn’t. If you add it correctly at the end, the next addition will replace the values left there.

But if you want to do it just put it before the cont-- the following:

cad[cont - 1].nome[0] = '\0';
cad[cont - 1].rua[0] = '\0';
cad[cont - 1].numero = 0;

Browser other questions tagged

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