What is the solution to this comparison?

Asked

Viewed 48 times

0

I am programming in C for a college subject, we must read a file and create some list methods to use in the program, basically I must implement the structure of a list and use in a solution for a requested program. My problem is that, my code works and is syntactically correct, but there is a comparison of two items that is not working, which is affecting the overall functioning of my program (because I should not enter information more than once in the list)I did debbugin and found that the finde function does not return at any time aux (which is a pointer), this causes my program to insert several times the same information (if I put in the file that the program reads), I have another problem too, my program reads strings with accentuation and even using locale, the application is not fixed, if you have any hints about it, it would be of great help. Follows my code:

General remarks, in my fscanf I have an if that checks if the find == null, that means that the find went through the whole list and did not find an item it returns null, if it finds what was passed at the end is found, it returns aux, thats my problem, if I put to insert 3 equal items, at the end it checks the function if (aux->item.name== x.name && aux->item.status == x.status), does not enter to return aux, which is entering repeated data in my program

1 answer

1


To compare strings you need to use a specific function, strcmp or strncmp (preferable).

TipoApontador find(TipoItem x, TipoLista* lista)
{
  TipoApontador aux;
  if (lista->primeiro != NULL)
  {
    aux = lista->primeiro;
    while (aux != NULL)
    {
      // if (aux->item.nome== x.nome && aux->item.estado == x.estado)
      //   return aux;
      if (strncmp(aux->item.nome, x.nome, 100) == 0 && strncmp(aux->item.estado, x.estado, 100) == 0)
        return aux;
      aux = aux->prox;
    }
  }
  return NULL;
}
  • In this case, to use strcmp you need to include the cstring library, my doubt is whether this is part of C, or C++. My work needs to be all in c

  • 1

    include the <string header. h>...to know which header to include for a function in C or C++ look in Google like this: "man function_name" (Linux), or "msdn function_name" (Windows)...use "strncmp" function, is safer than "strcmp"

  • Thank you very much !!

  • check this solution as accepted, so I gain a few more dots :) (click on the "ok" mark which is in gray below the bottom left fledgling)

Browser other questions tagged

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