Difficulties with Arrays

Asked

Viewed 60 times

0

Hello, Good Afternoon. I am a computer student and I am having difficulties with a part of a code, which is for the delivery of a work of my course.

void criar_cliente()
{
        /// Número do dia do aniversario, será armazenado como o id do usuário.    
        int id, j = 0;
        printf ("\nInsira o dia do seu aniversario: ");
        scanf ("%d", &id);

        /// Se acaso houver um id já presente no vetor, o programa informa esse erro para o usuário.
        if (id == clientes[j])
        {
            printf ("\n***Este Cliente já existe!***\n\n");
        }

        ///Se o id não tiver presente no vetor, o programa vai criar um novo cliente.
        else
        {
            j = j + 2;
            clientes [j] = id;
            printf ("\n\nCliente Criado com Sucesso!!!");
            printf ("\n***O numero digitado será o seu identificador. Portanto não o esqueça!\n\n\n\n\n\n");
        }
}

Client[ j ] is a vector that has 50 positions. This code is inside the "void creat_client()".

When I type a value, e.g.: 20, it stores in the first vector, when I type this value again, the program says that this number already exists. So far, the program meets the proposal I want. The problem is when I type a new value, e.g.: 3, instead of the program jumping to the next vector position, it updates the old value by putting 3 in its place.

Someone could help me with this code, I’m having it for a long time, and so far I haven’t found a solution. Thanks in advance.

  • You may not have posted the full code but in the post there are no definitions of the variables used. I believe that there is a lack of code in the research section and also no explanation of the reason for skipping positions in the vector.

  • The whole code is very big kkk, so I put only the part that is giving problems. I edited the code above by inserting the variables I forgot to put before. On jumping, it is in the sense that the vector only updates the values of position 0, rather than storing the next values at the following positions. EX: [ 0 ] = 20; [ 1 ] = 3, [ 2 ] = 8; What happens is that it only updates the values of vector 0, instead of putting the following values in the next vectors.

1 answer

1

The problem with your code is in the if part. Note that when the function starts the value of j is 0, then ALWAYS the comparison of if will be:

if(id == clientes[0])

Then the id value will only be compared to at the first position if the age is in client[1] the program will not compare with that id.

The problem about writing is that it will always put the new value at the j+2 position, as j ALWAYS will be 0 so the value will always be 0+2:

0 = 0 + 2;

The above operation is always done and the result will always be 2 and so your program on writes

A simple solution is to do a for to traverse the vector:

for(j = 0; j < 50; j++)
{
    if(id == cliente[j])
    {
        printf("Cliente já existe!");
    }
    else
    {
        clientes [j] = id;
        printf ("\n\nCliente Criado com Sucesso!!!");
        break;
    }
}

The above code will solve your problem, note that if will now compare to all positions within the client until it finds a location where the entered id does not exist, when this occurs then Else is activated and the id is stored at the position, the break serves to exit the loop prematurely. The above code will still have some errors in the future if you delete some client, the solution for this would be to sort the array, but this is already subject to another topic.

  • But does this structure allow me to create only one id, or several as 50 described by the vector ? The if-Else check, will run in the same way all 50 ?

  • 1

    This is to be placed in the function you showed, every time the function is called it will read 1 id and put in the client vector.

  • Got it, Thank you very much for your attention.

  • A question, if I wanted to register more values, as I should do ?

  • 1

    I don’t know if I understand, in case you add more values (birthday) to the client, right? Just call the function every time you want to add a new value, to avoid repeating code use a loop. But if you want to add other values to the client (not just the birthday) you would have to work with struct. Since I don’t have the complete code then I’m not sure how it works, but if you put the if I showed you in place of the if in the presented function then this function will always add a new id to a client unless the vector is full.

  • Ahh got it. Thank you very much!!!

Show 1 more comment

Browser other questions tagged

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