char array comparison returns false even for char[] equal - C

Asked

Viewed 26 times

-2

I am a beginner in C, I am doing a college activity and trying to compare a data obtained via scanf with a data saved in a registry array that I believe but even entering the same value, always returns false, independent of the comparison method.

my struct:

// Struct Veiculo
typedef struct
{
  char marca[50];
  char modelo[50];
  int anofab;
  Placa placa;
} Veiculo;

// Struct Veiculos (Container para veículo)
typedef struct
{
  int count;
  Veiculo v[10];
} Veiculos;

Where I get the value:

Veiculos frota;
char temp[50];
printf("Insira o modelo: ");
scanf("%s", &temp);
filtrarmodelo(&frota, temp);

Where do I compare:

void filtrarmodelo(Veiculos *frota, char modelo[50])
{
  printf("\n\n---------- [LISTANDO MODELO SELECIONADO] ----------\n\n");

  // Se não houver veículos, cai aqui
  if (frota->count == 0)
  {
    printf("\nNada para mostrar aqui :(\n\n");
  }

  else
  {
    for (int i = 0; i < frota->count; i++)
    {
      // Compara o modelo inserido com os modelos salvos anteriormente
      if (strcmp(frota->v[i].modelo, modelo))
      {
        printf("%s %s, %d. Placa %s-%s\n\n",
               frota->v[i].marca,
               frota->v[i].modelo,
               frota->v[i].anofab,
               frota->v[i].placa.letras,
               frota->v[i].placa.numeros);
      }
      else
      {
        printf("\nNenhum modelo encontrado :(\n\n");
      }
    }
  }
}

The output I got at the last run was that no model was found.

1 answer

3


The problem is that the function strcmp() returns the value 0 if the two strings are equal, and in C, the value 0 represents the false, then the right way to compare is: strcmp(stringA, stringB) == 0, this code returns true if both of them strings be equal.
Code working, adapted, because it lacked things in the question, with a test example:

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

typedef struct
{
    char marca[50];
    char modelo[50];
    int anofab;
} Veiculo;

typedef struct
{
    int count;
    Veiculo v[10];
} Veiculos;


void filtrarmodelo(Veiculos *frota, char modelo[50])
{
    printf("\n\n---------- [LISTANDO MODELO SELECIONADO] ----------\n\n");
    
    if (frota->count == 0)
    {
        printf("Nao ha carros na frota.\n");
        return;
    }
    else
        for (int i = 0; i < frota->count; i++)
        {
            printf("%s - %s\n", frota->v[i].modelo, modelo);
            if (strcmp(frota->v[i].modelo, modelo) == 0)
                printf
                (   
                    "%s %s, %d.\n",
                    frota->v[i].marca,
                    frota->v[i].modelo,
                    frota->v[i].anofab
                );
            else
                printf("Nenhum modelo encontrado.\n");
        }
}
int main()
{
    Veiculos frota;
    frota.count = 1;
    Veiculo veiculoNovo = { .marca = "Ab",  .modelo = "Cd", .anofab = 2022};
    frota.v[0] = veiculoNovo;

    char temp[50];
    printf("Insira o modelo: ");
    scanf("%s", temp);
    filtrarmodelo(&frota, temp);

    system("pause");
    return 1;
}


Input
Cd


Output

---------- [LISTANDO MODELO SELECIONADO] ----------

Cd - Cd
Ab Cd, 2022.
  • Gee, thanks a lot, pal. Now if you can explain something to me, why did my question get two negative votes? I don’t use the stack overflow much so I haven’t really caught the dynamics of the site. about the lack of information I thought it would be too much code and mess up the question so I put only the part involved in the problem. thanks again.

  • To improve your question you need to read the text of this link. And it also has the text next to the Page of creation of questions.

Browser other questions tagged

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