That’s what it is, but I don’t know what the error is ...

void del(item * q) { if (q == first) { first = first->next; } else { item * d = first; while (d->next!= q) { d = d->next; } d->next = q->next; if (q == last) last = d; delete q; } } void delMenu() { system("cls"); int ID; cout<<"Введите номер записи: "; cin>>ID; item * r = last; if(r!= NULL) { while(r->inf.id == ID && r->next!= NULL) { r=r->next; } if(r->inf.id == ID) del(r); else cout<<"Запись не найдена в базе\n"; } else { cout<<"Записей нет"; } _getch(); } 
  • @Valentin, next time, please format the code yourself. - northerner
  • @ Valentin, and what does not work? - AseN
  • yes, it’s just the compiler’s own error that they’re saying the wrong address, etc. and etc. - Valentine
  • What are specific compiler errors? And give more code. From the above, little can be figured out - carapuz

1 answer 1

@Valentin , if it crashes at runtime, then this is not called the “compiler error” (they say runtime error or just runtime error )

Judging by the words "the address is not the one," I can assume that this is a runtime error and an error somewhere in the construction of the list (perhaps last! = NULL, but it points to memory not allocated by new ).

Use the debugger. Helps a lot.

Generally you have a few mistakes here.

one). in delMenu in while you start with last (???), and from the code del () it follows that the list starts with first .

2). the condition in while (r-> inf.id == ID && r-> next! = NULL) is simply wrong (without taking into account error 1). Must be inf.id! = Id

3). in del () operator delete q; it is necessary to pull from else to the end (after all if ). In general, del () is done carelessly (assuming q is on the list).

And finally, this idea of ​​working with the list is ineffective. You run twice (on average half) on the list. If you delete (insert after, before) at the address of a list item, then make a doubly linked list. Then look for when performing such an operation is not necessary.

If for some reason you simply need a simply connected, then do the deletion not by address, but by ID , i.e. Combine search and delete. It will turn out something like

 if (!first) cout << "Нет записей"; else { cout<<"Введите номер записи: "; cin>>ID; if (!findDel(ID)) cout<<"Запись не найдена в базе\n"; } 
  • I thank for such an explanatory answer, I will understand - Valentine