Problems with CRUD in C (DELETE method)

Asked

Viewed 249 times

0

In the delete function, the user will inform an AR that he wants to delete, when the AR exists in the memory he deletes, that part of the code works... The problem is when it does not find in memory the RA... The system locks

void deletar(void){
    system("cls");
    //RA que o usuario quer deletar
    int n;
    printf("Digite o RA que deseja deletar: ");
    scanf("%i", &n);

    do{
        //Esta parte esta ok
        if(p->ra == n){
            //deleta a struct da memoria
            free(p);
            printf("\n\n\nAluno deletado com sucesso\n");
            system("pause");
            system("cls");
            //limpando a tela e voltando para o menu principal
            main();
        }

        //Quando ele nao acha o RA o programa trava.
        //unica coisa que falta pra finalizar o metodo
        if (p->anterior==NULL){     
            printf("\n\n\nERRO 404: RA nao existe no sistema\n");
            system("pause");
            system("cls");
            //limpando a tela e voltando para o menu principal
            main();
        }

        //vai pra proxima struct
        p = p->anterior;
    }while(1);
}

I put a condition for if the previous one is NULL means that it has already traversed all the structs, then it does not exist, but it is not working

  • assuming that "delete" was called by "main", then you use "Return" to go back to main, not the name of the main function, otherwise there is a recursive call

  • but I need you to go back to the main menu, the program can’t close after that, you understand?

  • worked by putting Return...

2 answers

2


The error is in the return of the "delete" function, where "main()" is (which makes no sense, as it would be a recursive call from the main program) should be "Return".

void deletar(void)
{
  system("cls");
  //RA que o usuario quer deletar
  int n;
  printf("Digite o RA que deseja deletar: ");
  scanf("%i", &n);

  do {
    //Esta parte esta ok
    if (p->ra == n) {
      //deleta a struct da memoria
      free(p);
      printf("\n\n\nAluno deletado com sucesso\n");
      system("pause");
      system("cls");
      //limpando a tela e voltando para o menu principal
      // main(); // <------------------------- ERROR!!!
      return;
    }

    //Quando ele nao acha o RA o programa trava.
    //unica coisa que falta pra finalizar o metodo
    if (p->anterior==NULL){     
        printf("\n\n\nERRO 404: RA nao existe no sistema\n");
        system("pause");
        system("cls");
        //limpando a tela e voltando para o menu principal
        // main(); // <--------------- ERRO!!!
        return;
      }

      //vai pra proxima struct
      p = p->anterior;
    } while (1);
}
  • but I need you to go back to the main menu, I tried the Return and the program ends... This can’t happen, understand?

  • Uai, you have adapt the logic of the main function so that the program does not end after calling a function of crud...you will probably need a loop, in this loop you will have a menu of options, with options C,R, U, D...and F at last..., you is that it has to define the logic of the program

  • Okay... got it, thanks

0

Instead of the delete function calling the main every time after its execution, I followed the @zentrunix advice and looped the main function, so when the delete function ends, it automatically goes back to main.

void deletar(void){
    system("cls");
    //RA que o usuario quer deletar
    int n=0;
    printf("Digite o RA que deseja deletar: ");
    scanf("%i", &n);

    while(p->anterior!=NULL){
        if(p->ra == n){
            //deleta a struct da memoria
            free(p);
            printf("\n\n\nAluno deletado com sucesso\n");
            system("pause");
            system("cls");
            //limpando a tela e voltando para o menu principal
            main();
        }

        p = p->anterior;
    }

    //Se nao entrar no if significa que nao foi encontrado o RA
    printf("\n\n\nERRO 404: RA nao existe no sistema\n");
    system("pause");
    //limpando a tela e voltando para o menu principal
    system("cls");
}

Browser other questions tagged

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