Check if the number typed is already present on the struc

Asked

Viewed 339 times

1

Good afternoon, I’m facing the following problem. I need to make an algorithm for registering bank accounts of 15 people with the following information: account number, name and balance. However it is necessary to verify if the account number has already been typed, if yes an error message will be printed if you will not request the rest of the data.

Only I’m not able to do this check, always giving the message that the number already exists even if it doesn’t exist. Follow my code:

#include <stdio.h>
#include <stdlib.h>
#define TAM 2

typedef struct {
    int num_conta;
    char nome[40];
    float saldo;
} Dados;

Dados Clientes[TAM];
int posConta = 0;

//Função para imprimir menu de opções
void Imprimir_Menu();

//Função para cadastrar clientes
void Cadastro(Dados *Clientes, int *pos);

int main(int argc, char** argv) {
    int opcao;


    do {
        Imprimir_Menu();

        printf("Digite a opção desejada: ");
        scanf("%d", &opcao);

        switch (opcao) {
            case 1:
                Cadastro(Clientes, &posConta);
                break;
        }
    } while (posConta != TAM);

    return (EXIT_SUCCESS);
}


//-----------------------------------------------------------------------------//

void Imprimir_Menu() {
    printf("MENU \n");
    printf("\t 1. Cadastrar contas; \n");
    printf("\t 2. Visualizar todas as contas de determinado cliente; \n");
    printf("\t 3. Excluir a conta com menor saldo; \n");
    printf("\t 4. Sair \n\n");
}

//-----------------------------------------------------------------------------//

void Cadastro(Dados *Clientes, int *pos) {
    int Num_temp, i, achou = 0;

    printf("\n--------------------CADASTRO DE CLIENTES--------------------\n");

    printf("%dº Cliente \n", *pos + 1);

    /* O programa iria prosseguir somente quando o usuario digitar um número de conta
     que ainda não existe */

    do {
        printf("\tDgite o número da conta: ");
        scanf("%d", &Num_temp);

        for (i = 0; i < *pos + 1; i++) {
            if (Clientes[i].num_conta == Num_temp) {
                printf("achou = %d \n", achou);
                achou = 1;
            }
        }
        if (achou = 1) {
            printf("Conta ja existente. Favor digitar um número diferente. \n");
        }
    } while (achou != 0);

    Clientes[*pos].num_conta = Num_temp;

    printf("\tDgite o nome do cliente: ");
    setbuf(stdin, NULL);
    gets(Clientes[*pos].nome);

    printf("\tDigite seu saldo: ");
    scanf("%f", &Clientes[*pos].saldo);

    *pos = *pos + 1;


    printf("---------------------------------------------------------------------\n");
}

//-----------------------------------------------------------------------------//

Follow the output of the program:

inserir a descrição da imagem aqui

  • 2

    if (achou = 1). The compiler performs miracles. Always compile the code and pay close attention to the warnings, as 99.99% of them are errors. Naturally the code has other problems, but I am focusing only on what has been quoted

  • 1

    The most important comment from @Isac is that the code has several problems.

  • I imagined, I would learn these contents on my own during my college holidays, to have a good base in the 2nd Period. Yet orbed to all who helped me find the error.

1 answer

1

Good friend for what I saw in the condition If(found = 1), you do found receive 1, and did not find==1

  • I can’t believe that’s all, there was a lot of pressure on that kk!

  • What semester are you in? Because from what I’m seeing there are enough errors in your code.

  • I’m going for second

  • Well, I’m in the fifth semester... I’ve already won twice in first place my college’s programming marathon... If you need help can send me e-mail or message.

  • I do not dispense no Victor, mt thank you. As you have more experience than me, what do you think I need to improve on my codes? Which error most found in my code above?

Browser other questions tagged

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