Help me fix the code please.

The program works correctly, but after displaying the desired item just hangs.

void find() { car *p, *temp; char sp[10]; clrscr(); printf("Vvedite model' mashini,kotoruiu hotite naiti: "); scanf("%s", &sp); p = head; while (p == NULL) { if(strcmp((p->model), sp) == 0) { printf("[Marka]\t[Model]\t[Cvet]\t[Strana]\t[Cena]\n"); printf("%s", p->marka); printf("\t%s\t", p->model); printf("%s\t", p->cvet); printf("%s", p->strana); printf("\t\t%s\n", p->cena); p = p->next; } } } 

I use Borland C 3.1, I write in C. Thank you in advance

  • You go to the next element in the loop only if strcmp returns 0. Therefore, the loop after the found element is trampled in place. 30 seconds in the debugger would save you 5 minutes on registration and question wording. - VladD pm
  • I think in while (p==NULL) you need instead of == to write != (Or just while (p) { ... or also stereotypically for (; p; p = p->next) { ... - avp
  • need to change if to while ? - adlo
  • I changed p==NULL to p!=NULL , I just forgot to change it here. The problem is the same .. - adlo
  • help please, I still do not understand what needs to be done - adlo

1 answer 1

You go to the next element in the loop only if strcmp returns 0. Therefore, the loop after the found element is trampled in place. 30 seconds in the debugger would save you 5 minutes on registration and question wording.