Program in C closes after execution of procedure

Asked

Viewed 72 times

-1

Boas, The thing is, I’m developing a program that is for evaluation, which is to do a drug management...the goal is to have the features to add and remove medicines, as well as their listing... I am with the problem that when choosing the list the program lists everything well.... but closes then. Here’s the code snippet, thank you.

    int main(){
      setlocale(LC_ALL,"Portuguese");

      char *categorias[20] = {"Anti-Infeciosos", "Sistema Nervoso Central", "Aparelho Cardiovascular", "Sangue", "Aparelho Respiratório",\
                                    "Aparelho Digestivo", "Aparelho Geniturinário", "Doenças Endócrinas", "Aparelho Locomotor", " Medicação Antialérgica",\
                                    "Nutrição", "Corretivos da Volemia", "Afeções Cutâneas", "Afeções Otorrinolaringológicas", "Afeções Oculares",\
                                    "Antineoplásticos", "Intoxicações", "Vacinas", "Meios Digestivos", "Material Diverso"};

      char *viasAdministracao[10] = {"Oral", "Injetável", "Dermatológica", "Nasal", "Oftálmica", "Respiratória", "Auricular", " Sublingual",\
                                     "Intramuscular", "Retal"};
      int nMedicamentosInserido = 0;
      char opcao;
      struct medicamentos medicamento[2000];
      int armario [20][10], bit[20][10][10];
      int nr;

      do{

    printf("\n\n\t FARMÁCIA\
              \n1 Administração\
              \n2 Venda de Medicamentos\
              \n3 Listagem de Medicamentos\
            \n\n4 SAIR");
    printf("\n\nInsira uma opção: ");
    scanf(" %c",&opcao);

    switch (opcao) {

      case '1':{
        printf("\n\n1 Inserir medicamentos\
                  \n2 Gerir medicamentos fora de validade\
                  \n3 Inserir sócio");
        printf("\n\tInsira uma opção: ");
        scanf(" %c",&opcao);

        if(opcao == '1'){

          int categ, viaz;

          printf("\nInsira a sua categoria: ");
          scanf(" %i",&categ);
          fflush(stdin);

          printf("\nInsira a via de administração: ");
          scanf(" %i",&viaz);
          fflush(stdin);

          nr= (categ * 100) + (viaz * 10);

          if(armario[medicamento->categoria][medicamento->viaAdministracao] != 10){
          inserirMedicamento(&medicamento[nr + armario[medicamento->categoria][medicamento->viaAdministracao]], armario, bit, nMedicamentosInserido, categ, viaz);
          //nMedicamentosInseridos = nMedicamentosInseridos + MEDICAMENTO.quantidade;
          printf("\n%s",medicamento[nr].nomeMedicamento);

          nMedicamentosInserido++;


        }
        }
        break;
      }

      case '2':{
        vendaMedicamento(&medicamento[nr - armario[medicamento->categoria][medicamento->viaAdministracao]], armario, bit, categorias, viasAdministracao, nMedicamentosInserido);
        break;
      }

      case '3':{
        printf("\n\n1 Listagem de todos os medicamentos\
                  \n2 Listagem por categoria\
                  \n3 Listagem por via de administração\
                  \n4 Listagem por data de validade");
        printf("\nInsira uma opção: ");
        scanf(" %c",&opcao);

        if(opcao == '1'){
        listagemMedicamentos(medicamento, categorias, viasAdministracao);
        }
        if(opcao == '2'){
        listagemCategoria(medicamento, categorias, viasAdministracao, nMedicamentosInserido);
        }
        if(opcao == '3'){
        listagemVia(medicamento, categorias, viasAdministracao, nMedicamentosInserido);
        }
        if(opcao == '4'){
        listagemData(medicamento, categorias, viasAdministracao, nMedicamentosInserido);
        }
        break;
      }
    }
  }while (opcao != 0);
  return 0;
}


    void listagemMedicamentos(struct medicamentos* medicamento,char* categorias[], char* viasAdministracao[]){
    for( size_t i = 0; i < 2000; i++){
      if(medicamento[i].validade.mes != 0){
                 printf("\n\n\tNome: %s\n\tCategoria: %s\n\tVia de administração: %s\n\tData de validade: %i/%i/%i\n\tQuantidade: %i\n\tPreço: %1.f",\
                        medicamento[i].nomeMedicamento, categorias[medicamento[i].categoria], viasAdministracao[medicamento[i].viaAdministracao], medicamento[i].validade.dia,\
                        medicamento[i].validade.mes, medicamento[i].validade.ano, medicamento[i].quantidade, medicamento[i].preco);
          }
        }
      }
  • I am not an expert but there is no system ("pause"); after data display?

  • The same happens :/

  • Post your main and complete routine. Did you include stdlib. h library? You can also try : printf("Press ENTER n"); getchar(); Return 0;

  • already posted, not include I will try thank you :)

  • did not work :/

  • Well, 10 years ago it was one of those two ways that I did or used getchar to force a user in and out (I/O) operation or used the pause system to force the program to wait. If you choose system("PAUSE"); you must include the stdlib library. h the method is native to it. Otherwise this function should not have a Return. And finally edits his question the identation became very messy little is understood. But one detail is running on linux if you are using getchar system("PAUSE") is for windows DOS.

  • not a function but a procedure. Thanks for the opinion I will change the title. I am doing in windows

Show 2 more comments

1 answer

0

As there are only excerpts of the code, there is no way to track everything, but if you do not receive an error message, it is because there is a logic problem.

Its input says that to exit the user inserts 4 and stores it in the option variable, but at the end of the {} while block it says to repeat while option is different from 0, ie your program has no output condition.

You reuse the option variable during cases 1 and 3, and test this variable outside the scope of case no do {} while, generating unforeseen behavior.

It is likely that at some point you are assigning the value 0 to the option, and this leads the program to terminate when it arrives at the end of {} while.

Recommendations: Use a different control variable for each flow control. Each switch must have its variable. Avoid this type of error.

I don’t like {} while, because it separates the marker from the repetition condition block, but it is valid.

  • thanks for the help. this delivery of the do while was my lapse the condition already corrected, I will follow your recommendation, which recommends instead of the while?

  • Instead of do { código } while condição I prefer while condição { código }, mainly for large blocks like this.

Browser other questions tagged

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