error when searching client

Asked

Viewed 35 times

1

i made this program my problem is being in researching the client so I research it does not give me the result and terminates the program, I would like to know what I do to remedy the problem

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


typedef struct cliente CLIENTE;

struct cliente{
    unsigned long  id;
    char nome[50];
    char telefone[15];
    char cpf[12];
    char email[50];

};

void menu();
void cadastracliente();
void listarCliente();
void pesquisaCliente();

int main(){
    menu();
    return 0;
}

void menu(){

    int escolha;
    do{
        system("cls");
        printf("[1]-cadastra Cliente\n");
        printf("[2]-listar Cliente\n");
        printf("[3]-Pesquisar Cliente\n");
        printf("[0]-Sair\n");
        printf(": ");
        scanf("%d",&escolha);
        switch(escolha){
        case 1:
            cadastracliente();
            break;
        case 2:
            listarCliente();
            break;
        case 3:
            pesquisaCliente();
            break;
        }

    }while(escolha!=3);
}

void cadastracliente(){
    system("cls");
    FILE* arquivo;
    CLIENTE clt;

    arquivo = fopen("cliente.dat","ab");
    if(arquivo ==NULL){
        printf("Problemas na abertura do arquivo");
    }else{
        do{

            fflush(stdin);
            printf("Digite o nome: ");
            gets(clt.nome);
            fflush(stdin);
            printf("Digite o CPF: ");
            gets(clt.cpf);
            fflush(stdin);
            printf("Digite o email: ");
            gets(clt.email);
            fflush(stdin);
            printf("Digite o Telefone: ");
            gets(clt.telefone);

            fwrite(&clt,sizeof(CLIENTE),1,arquivo);

            printf("\nDeseja continuar(s/n)");

        }while(getch() =='s');
        fclose(arquivo);
    }
}


void listarCliente(){
    system("cls");
    FILE* arquivo;
    CLIENTE clt;

    arquivo = fopen("cliente.dat","rb");
    if(arquivo ==NULL){
        printf("Problemas na abertura do arquivo");
    }else{
        printf("---------------CLIENTES---------------\n");
        while(fread(&clt,sizeof(CLIENTE),1,arquivo)==1){

            printf("id: %lu\n",clt.id);
            printf("Nome: %s\n",clt.nome);
            printf("CPF: %s\n",clt.cpf);
            printf("E-mail: %s\n",clt.email);
            printf("Telefone: %s\n",clt.telefone);
            printf("--------------------------------------\n");
        }
    }
    fclose(arquivo);
    getch();

}

void pesquisaCliente(){
    system("cls");
    FILE* arquivo;
    CLIENTE clt;
    char nome[30];

    arquivo = fopen("cliente.dat","rb");
    if(arquivo ==NULL){
        printf("Problemas na abertura do arquivo");
    }else{
        fflush(stdin);
        printf("Digite o nome a pesquisar: ");
        gets(nome);

        while(fread(&clt,sizeof(CLIENTE),1,arquivo)==1){
            if(strcmp(clt.nome,nome)==0){
                printf("id: %lu\n",clt.id);
                printf("Nome: %s\n",clt.nome);
                printf("CPF: %s\n",clt.cpf);
                printf("E-mail: %s\n",clt.email);
                printf("Telefone: %s\n",clt.telefone);
                printf("--------------------------------------\n");
            }
        }
    }
    fclose(arquivo);
    getch();
}

´´´
  • I found no problem in your code. Just this line while(escolha!=3);. just replace 3 with 0, as indicated in my.

  • thank you very much.

2 answers

0


There are some problems in the code that are making it behave like this. The first thing, as Bernardo said in the comments is that you need to change the while(escolha!=3) for while(escolha!=0) in the main loop. This will correct the menu flow.

The second thing to check is that you give no indication that the customer has not been found. So if you search for a client that doesn’t exist, nothing happens and the program gets stuck on the screen, locked. A flag can solve this easy, easy.

char encontrado = 0;
while (fread(&clt, sizeof(Cliente), 1, arquivo) == 1) {
    if (strcmp(clt.nome, nome) == 0) {
        exibirCliente(&clt);
        encontrado = 1;
        break;
    }
}
if (!encontrado)
    printf("Cliente %s não encontrado.\n", nome);

Another thing is that, as it is, the customer will only be found if you search by name exactly as it was registered, taking into account uppercase, lowercase, spaces and special characters. If you pay attention, fgets is saving in the file the texts with character n, so you will need to remove it from the client fields before saving. A simple function to do this can be implemented with a loop:

void removerEnter(char* buffer, int size) {
    for (int i = 0; i < size; i++) {
        if (buffer[i] == '\n') {
            buffer[i] = '\0';
            return;
        }
    }
}

void ajustarCliente(Cliente* cliente) {
    removerEnter(cliente->nome, 50);
    removerEnter(cliente->telefone, 15);
    removerEnter(cliente->cpf, 12);
    removerEnter(cliente->email, 50);
}

and call before saving it:

do {
    printf("Digite o nome: ");
    fgets(clt.nome, 50, stdin);

    printf("Digite o CPF: ");
    fgets(clt.cpf, 12, stdin);

    printf("Digite o email: ");
    fgets(clt.email, 50, stdin);

    printf("Digite o Telefone: ");
    fgets(clt.telefone, 15, stdin);

    ajustarCliente(&clt);
    fwrite(&clt, sizeof(Cliente), 1, arquivo);

    printf("\nDeseja continuar(s/n)\n");

} while (getch() == 's');
fclose(arquivo);

A complete implementation with all these fixes looks something like this:

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

struct cliente {
    unsigned long id;
    char nome[50];
    char telefone[15];
    char cpf[12];
    char email[50];
};
typedef struct cliente Cliente;

void menu();
void cadastracliente();
void listarCliente();
void pesquisaCliente();

void exibirCliente(Cliente* cliente) {
    printf("id      : %lu\n", cliente->id);
    printf("Nome    : %s\n", cliente->nome);
    printf("CPF     : %s\n", cliente->cpf);
    printf("E-mail  : %s\n", cliente->email);
    printf("Telefone: %s\n", cliente->telefone);
}

void removerEnter(char* buffer, int size) {
    for (int i = 0; i < size; i++) {
        if (buffer[i] == '\n') {
            buffer[i] = '\0';
            return;
        }
    }
}

void ajustarCliente(Cliente* cliente) {
    removerEnter(cliente->nome, 50);
    removerEnter(cliente->telefone, 15);
    removerEnter(cliente->cpf, 12);
    removerEnter(cliente->email, 50);
}

int main() {
    menu();
    return 0;
}

void menu() {
    int escolha;
    do {
        system("cls");
        printf("[1]-cadastra Cliente\n");
        printf("[2]-listar Cliente\n");
        printf("[3]-Pesquisar Cliente\n");
        printf("[0]-Sair\n: ");
        fscanf(stdin, "%d%*c", &escolha);
        switch (escolha)
        {
        case 1:
            cadastracliente();
            break;
        case 2:
            listarCliente();
            break;
        case 3:
            pesquisaCliente();
            break;
        }
    } while (escolha != 0);
}

void cadastracliente()
{
    system("cls");
    FILE* arquivo;
    Cliente clt;

    arquivo = fopen("cliente.dat", "ab");
    if (arquivo == NULL)
    {
        printf("Problemas na abertura do arquivo");
        return;
    }

    do {
        printf("Digite o nome: ");
        fgets(clt.nome, 50, stdin);

        printf("Digite o CPF: ");
        fgets(clt.cpf, 12, stdin);

        printf("Digite o email: ");
        fgets(clt.email, 50, stdin);

        printf("Digite o Telefone: ");
        fgets(clt.telefone, 15, stdin);

        ajustarCliente(&clt);
        fwrite(&clt, sizeof(Cliente), 1, arquivo);
        printf("\nDeseja continuar(s/n)\n");

    } while (getch() == 's');
    fclose(arquivo);
}

void listarCliente()
{
    system("cls");
    FILE* arquivo;
    Cliente clt;

    arquivo = fopen("cliente.dat", "rb");
    if (arquivo == NULL) {
        printf("Problemas na abertura do arquivo!");
        return;
    }

    printf("---------------CLIENTES---------------\n");
    while (fread(&clt, sizeof(Cliente), 1, arquivo) == 1) {
        exibirCliente(&clt);
        printf("--------------------------------------\n");
    }
    fclose(arquivo);
    getch();
}

void pesquisaCliente()
{
    system("cls");
    FILE* arquivo;
    Cliente clt;
    char nome[30];

    arquivo = fopen("cliente.dat", "rb");
    if (arquivo == NULL) {
        printf("Problemas na abertura do arquivo");
        return;
    }

    printf("Digite o nome a pesquisar: ");
    fflush(stdin);
    fgets(nome, 30, stdin);
    removerEnter(nome, 30);
    char encontrado = 0;
    while (fread(&clt, sizeof(Cliente), 1, arquivo) == 1) 
    {
        if (strcmp(clt.nome, nome) == 0) {
            exibirCliente(&clt);
            encontrado = 1;
            break;
        }
    }
    if (!encontrado)
        printf("Cliente %s não encontrado.\n", nome);
    fclose(arquivo);
    getch();
}

I hope I’ve helped.

Good studies!

  • 1

    It helped a lot thanks

  • Imagine... it’s a pleasure. If you’ve solved your question, could you mark my answer as correct? Thanks!

  • How could I make a quote for customer ID? ,for client to have a different ID than another

0

while condition is wrong, in the code if you do a search will enter while and exit loop

while(choice!= 0);

Only change the condition to 0

Browser other questions tagged

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