Code matching verification on a vector within a C struct

Asked

Viewed 121 times

1

next, I created a struct

 struct eventos
{
    int codigo;
    char data[11];
    char atracao[50];
    int ingresso;
    float valoring;
};

And then I created a vector that way

struct eventos ficha[5];

My goal is to verify within a function called query(), if a code provided by the user corresponds to the code of an event already registered previously (In this program, there is the possibility of registering an event and one of the categories of the record is the event code), and then, after this check, I display the event data through another function called display(). I created query() as follows:

void consulta()
{
    printf("Digite o codigo do evento que deseja consultar\n ");
    scanf("%d", &code);
    for(h=0;h<2;h++)
        {
            if(code == ficha[h].codigo)
            {
                exibir();
                system("pause");
                system("cls"); 
            }

                else 
                {
                    printf("Evento nao cadastrado\n ");
                    system("pause");
                    system("cls"); 
                }
        }
}

However, a problem persists, every time I search for a code, if it is in the first positions of the vector, the event data is displayed, followed by, for example, two messages of "Event not registered". Similarly, if the code is in the last positions, it is displayed first "Event not registered", to finally display the event information corresponding to the code entered. I would like to know how to make the program break the loop as soon as it finds a valid code and display the event data. I tried to use the "break;" function, but somehow it works for the first test, but when, when registering two events, I get success consulting the first and the second is listed as an unregistered event.

If you can help me with some way out, I’d be very grateful!

1 answer

0


I made some changes in your function consult:

void consultar(){
    printf("Digite o codigo do evento que deseja consultar\n ");
    int code,posicao,achou=0;
    scanf("%d", &code);
    for(int h=0;h<2;h++){
        if(code == ficha[h].codigo){
            achou=1;
            posicao=h;
        }
    }

    if(achou == 1){
        printf("%d",ficha[posicao].codigo); 
        printf("Evento cadastrado\n ");                
        system("pause");
    }else{
        printf("Evento nao cadastrado\n ");
        system("pause");            
    } 
}

The function now works as follows, if it finds it adds +1 to a variable and another holds the position.

If the variable found=0, does not have this code in the vector, if the variable=1 exists this code in the vector.

Your mistake is:

You are doing the logical test at each vector position, so if in that position you have the code you are looking for, it will be true, if there is no false.

Browser other questions tagged

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