struct - C++ program doesn't run correctly -
i have written note program. reads notes, keeps them , allows user delete, select or update note. it's compiling , running, doesn't run correctly.
i have struct:
struct list { char title[101]; char text[501]; int cont; struct list* next; };typedef list list;
and these functions:
list* insert (list *l, char ti[101], char te[501]) { list* new = (list*) malloc(sizeof(list)); strcpy (new -> title, ti); strcpy (new -> text, te); new -> next = l; new -> cont = id; id++; return (new); } list* delete (list* l, int v) { list *ant = null; list *p = l; while (p != null && p -> cont != v) { ant = p; p = p -> next; } if (p == null) return l; if (ant == null) l = p -> next; else ant -> next = p -> next; free(p); return l; } void print (list *l) { list *p; (p = l; p != null; p = p -> next){ cout << "\ntitle: " << p -> title << "\n"; cout << "text: " << p -> text << "\n"; cout << "code: " << p -> cont << "\n" << "\n"; } }
on int main
have inserted , printed couple of times , worked fine. when want delete note, doesn't delete nor error code. yesterday working fine, today when got test it, nothing works right. can't understand why working , stopped.
as requested, main program:
list* ini(){ return (null); } int main() { list *l; char title[101]; char text[501]; char v; list* l1 = ini(); cout << "\ntitle: "; gets(title); cout << "text: "; gets(text); l1 = insert (l1,title,text); fflush(stdin); cout << "\ntitle: "; gets(title); cout << "text: "; gets(text); l1 = insert (l1,title,text); fflush(stdin); cout << "\ntitle: "; gets(title); cout << "text: "; gets(text); l1 = insert (l1,title,text); print(l1); cout << "delete: "; cin >> v; l1 = delete(l1, v); print(l1); return(0); }
note: rewrote code not translations, delete
valid function called deleteitem
.
your immediate issue this:
char v; //... cin >> v; l1 = deleteitem(l1, v); // <-- v char,
but
list* deleteitem (list* l, int v) {
you passing char
variable deleteitem
when should passing int. change type of v
int
.
what happening char
being translated int. if enter 1
, being sent 49, since ascii value of 1
49.
one of things c++ allows declare variables close point of use. if had declared v
closer deleteitem
function call, may have spotted mistake yourself.
Comments
Post a Comment