Search code examples
clinked-liststrcmp

strcmp won't return 0 when comparing string to a linked list data element


I'm having some trouble with a linked list program in which strcmp never returns 0, however they are equal. I tried strcpy() function to put temp -> name to a string, but it didn't work either. I'm trying to assign the average value to temp -> average.

Here is the input:

vloz Mrkvicka Jozko 1 1.25
vloz Hrusticka Ferko 2 1.5
vloz Kalerab Jurko 1 2.14
vloz Hrusticka Ferko 1 2.8
vloz Zeler Misko 1 4.12
vypis
zmen Hrusticka Ferko 3.0
vypis

My output should look like this:

firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25

firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=3.00           //current 2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=3.00           //current 1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25

My output currently:

firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25

firstname=Zeler, name=Misko, year=1, average=4.12
firstname=Hrusticka, name=Ferko, year=1, average=2.80 
firstname=Kalerab, name=Jurko, year=1, average=2.14
firstname=Hrusticka, name=Ferko, year=2, average=1.50
firstname=Mrkvicka, name=Jozko, year=1, average=1.25

Here are my full code:

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

struct student
{
  char firstname[50];
  char lastname[50];
  int year;
  double average;
  struct student *next;
};

struct database
{
  struct student *first;
};

struct database *create_head ()
{
  struct database *head = (struct database *)malloc(sizeof(struct database));
  head -> first = NULL;
  return head;
}

void vloz (struct database *head)
{
  struct student *temp = (struct student *) malloc (sizeof(struct student));
  temp -> next = NULL;

  char firstname[50];
  char lastname[50];
  int year;
  double average;

  scanf("%s %s %d %lf\n", firstname, lastname, &year, &average);
  strcpy(temp -> firstname, firstname);
  strcpy(temp -> lastname, lastname);
  temp -> year = year;
  temp -> average = average;

  temp -> next = head -> first;
  head -> first = temp;
  return; 

}

void zmen (struct database *head)
{
  char firstname[50];
  char lastname[50];
  double average;
  scanf("%s %s %lf", lastname, firstname, &average);

  struct student *temp =  head -> first;

  while (temp != NULL)
  {
    if (strcmp(temp -> firstname, firstname) == 0)
    {
      printf("TTT\n");
      temp -> average = average;
    }

    temp = temp -> next;
  }
}

void vypis (struct database *head)
{
  struct student *temp = head -> first;
  while (temp != NULL)
  {
    printf("lastname=%s, firstname=%s, year=%d, average=%.2lf\n", temp -> firstname, temp -> lastname, temp -> year, temp -> average);
    temp = temp -> next;
  }
  printf("\n");
}


int main()
{
  char prikaz[20];
  struct database *head = create_head();
  while (scanf("%s", prikaz) > 0)
  {
    if (strcmp(prikaz, "vloz") == 0)
    {
      if (head->first != NULL)
      {
        struct database *head = create_head();
      }
      vloz (head);
    }

    if (strcmp(prikaz, "vypis") == 0)
      vypis (head);

    if (strcmp(prikaz, "zmen") == 0)
      zmen (head);


  }

  return 0;
}

Solution

  • You switched first name and last name, therefore the compare does not work.

    In vloz it looks like this:

    scanf("%s %s %d %lf\n", firstname, lastname, &year, &average);
    

    And in zmen the first element of scanf is lastname:

    scanf("%s %s %lf", lastname, firstname, &average);
    

    If you switch the order in zmen, so that it looks like:

    scanf("%s %s %lf", lastname, firstname, &average);
    

    you will get the expected output.

    Debug Output

    When using the correct order the output in the debug console looks like the expected values:

    debug output