O
O
olkhovich2019-03-21 15:39:55
C++ / C#
olkhovich, 2019-03-21 15:39:55

What is the sort error?

Please help me find the error.
It is required to display the titles of books of a certain author (apparently, you need to enter the author's surname from the keyboard), published after 2000.
When I enter any last name, "No" is displayed

void year (list begin)
{
     char surname [15];
     int year, k = -1;
     list pos;
     system ("CLS");
     if (!begin) // если список пуст
     {
      puts ("List is empty");
      system("pause");
      return;
     }
     getchar();
     puts ("Surname?");
     scanf ("%c", &surname);
     while (begin) //пока в списке есть эл-ты
     {
        k++;
        if (begin->data.surname == surname && begin->data.year > 2000)
        {
           year = begin->data.year;
           pos = begin;
        }
        begin = begin->next; // переставляем указатель на след эл-т
//        free (temp); // удаляем первый
     }
     if (year <= 0 || year >= 2020) puts ("No");
     else printf ("Title : %s\n", pos->data.title);
     system("pause");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2019-03-21
@olkhovich

apparently you need to enter the author's last name from the keyboard

https://en.cppreference.com/w/c/string/byte/strcmp
this is never executed
if (begin->data.surname == surname && begin->data.year > 2000) // false false false false ...
{
  year = begin->data.year;
  pos = begin;
}

what is equal then int year???
and correspondingly
Somehow
while(begin) //пока в списке есть эл-ты
{
  if(begin->data.year > 2000 && !strcmp(begin->data.surname, surname))
  {
    printf("Title : %s\n", begin->data.title);
  }
  begin = begin->next; 
}

если, вам это занятие интересно, то лучше
Книжку купите/скачайте

S
Stalker_RED, 2019-03-21
@Stalker_RED

The encoding is incorrect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question