Difficulty locating vector value in C

Asked

Viewed 35 times

-1

good night. I’ll be direct..

  1. I’m an ADS student and I’m in my second semester..

  2. I need to develop a rotating parking system. In this the user needs to insert the vehicle plate and we must assign to it whether it is aged, pne or standard, and also the time of entry of the vehicle in the parking. However I am not able to do even the first part of the exercise that would register a plate and then find it in the vector where it was registered.

Follow the code and ask me to help me find where I’m going wrong.

#include <stdio.h>
#include <stdlib.h>
char placa[5];
int op;

void cadastro ();
void pesquisaPlaca ();

int main () {
    
cadastro ();
pesquisaPlaca ();

return 0;
}

void cadastro (){
do {
    char placa1;
    printf ("\ndigite a placa do veiculo:");
    scanf ("%s",&placa[placa1]);
    printf ("digite 1 para novo cadastro ou 0 para sair:");
    scanf ("%d",&op);
    placa1++;
} while (op==1);
    
}

void pesquisaPlaca (){
    char placaPesq;
    int j;
    
    printf ("\ninsira a placa a ser pesquisada:");
    scanf ("%s",&placaPesq);
    fflush(stdin);
    
    for (j=0;j<=placa;j++) {
        if (strcmp(placa[j], placaPesq)) {
            printf ("\nplaca %s encontrada.", placa[j]); // nao localiza a placa
        } else {
            printf ("\nplaca nao encontrada");
        }
    }
}
  • Note that here: char placa1; printf ("\ndigite a placa do veiculo:"); scanf ("%s",&placa[placa1]); you declare placa1 but does not assign any value to the variable. When using in placa[placa1] the variable placa1 will contain memory junk.

1 answer

0

Some tips:

Here you only keep a 5-character board;

char placa[5];

You need a plate array that contains the number of the plates, hint:

#define TAMANHO_PLACA 5
#define MAXIMO 10

char placas[MAXIMO][TAMANHO_PLACA];

In the register you need to follow the same idea and use the board size

int i = 0;
do{
    char tmp[TAMANHO_PLACA];
    printf ("\ndigite a placa do veiculo:");
    scanf ("%s",tmp);
    strcpy(&placas[i][0], tmp);
    i++;
}while(opt != 0 || i == MAXIMO);

to search, just go through the array of plates comparing to the one you want:

char tmp[TAMANHO_PLACA];
printf ("\ndigite a placa do veiculo:");
scanf ("%s",tmp);
for(int i = 0; i <= MAXIMO; i++)
{
    if (strstr(placas[i], tmp) != NULL)
        printf ("achou:");
   
}

Browser other questions tagged

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