daisygirl 0 Report post Posted November 27, 2012 Salam 1 Malaysia, coding ini berkenaan dengan menu masukkan data, hapuskan data, membuat carian data, dan paparkan data. Tetapi apabila coding diljalankan, pemilihan menu tidak boleh dibuat, coding ini hanya dapat paparkan menu sahaja. Saya tidak pasti masalahnya dimana. Minta jasa baik member-mamber tolong semakkan dan beritahu saya masalahnya di mana. terima kasih. [CODE] #include <iostream.h> using namespace std; struct abc { string name,ic,dte_b; abc *next; //pointer next=point to next node. }; void insert(); void remove(); void search(); void display();// declare operation of list abc *head; //head is a pointer which will point to first node. main() { int p; char respond; cout<<"________________________________________________"<<endl; cout<<"\n\nCUSTOMER INFORMATION SYSTEM OF ABC COMPANY "<<endl; cout<<"________________________________________________"<<endl; cout<<"\n\nMENU"<<endl;; cout<<"\n1) CUSTOMER DATA INPUT"<<endl; cout<<"2) CUSTOMER DATA DELETE"<<endl; cout<<"3) CUSTOMER DATA SEARCH"<<endl; cout<<"4) CUSTOMER DATA DISPLAY"<<endl; cout<<"5) EXIT "<<endl; while(respond == 'Y' || respond == 'y') { cout<<"CHOICE : "; cin>>p; //enter no. of menu chosen. if (p==1) { cout<<"-----------------------------------"<<endl; cout<<"CUSTOMER DATA INPUT"<<endl; cout<<"-----------------------------------"<<endl; insert();//call function insert. } else if (p==2) { cout<<"-----------------------------------"<<endl; cout<<"CUSTOMER DATA DELETE"<<endl; cout<<"-----------------------------------"<<endl; remove();//call function delete. } else if (p==3) { cout<<"-----------------------------------"<<endl; cout<<"CUSTOMER DATA SEARCH"<<endl; cout<<"-----------------------------------"<<endl; search();//call function search. } else if (p==4) { cout<<"-----------------------------------"<<endl; cout<<"CUSTOMER DATA DISPLAY"<<endl; cout<<"-----------------------------------"<<endl; display();//call function display. } else { cout<<"-----------------------------------"<<endl; cout<<"EXIT"<<endl; cout<<"-----------------------------------"<<endl; } cout<<"BACK TO MENU [Y/T] :"; cin>>respond; } } //function for insert data into list void insert() { abc *newptr; abc *cur , *prev; // create new pointer for struct node cur = head; //new value inserted (cur) = niai head prev = NULL; //last value or before (prev)=nilai NULL newptr = new abc; //create new node to be insert. //assign value for node cout<<"ENTER NAME : "; cin>>newptr->name; //new pointer point to new value inserted (name). newptr->next = NULL; //new value point to NULL. cout<<"ENTER IC NUMBER : "; cin>>newptr->ic; //new pointer point to new value inserted (ic). newptr->next = NULL; //new value point to NULL. cout<<"ENTER DATE OF BIRTH : "; cin>>newptr->dte_b; //new pointer point to new value inserted (date of birth). newptr->next = NULL; //new value point to NULL. //link and head if (head == NULL) // first, if list is empty, { head = newptr ; //head will point to newptr } else // or else insert at front. { //move the cur dan prev pointer while(cur!= NULL && newptr->name > cur->name)//when cur is not NULL, new pointer which point //to nama value will give current pointer to move the nama value. { prev = cur; //previous value equal to current value cur = cur->next; //current value point to next node. } // insert at front newptr->next = head; //new pointer point to next node and equal to head. head = newptr; //the value of head is equal to value of new pointer. } } //function to delete the data void remove() { string ic; abc *prev; abc *cur; cout<<"ENTER IC NUMBER OF CUSTOMER DATA TO BE DELETED : "; cin>>ic; prev = NULL; cur = head; if(head == NULL) // start with empty list. cout<<"SORRY, NO DATA FOUND IN THE LIST:"; else //if list has a data,can be delete. { while(cur != NULL && ic!= cur->ic) //move the pointer of cur and prev { prev = cur; cur = cur->next; } if(prev == NULL) //remove from front. { head = head->next; // or head = cur->next delete cur ; //atau free(cur); } else if (cur == NULL) //if no elemen in the list { cout<<"SORRY, NO DATA TO BE DELETED IN THE LIST."; } else //remove from center or back. { prev->next = cur->next; delete cur; } } } //function untuk search data. void search() { string ic; abc *cur; cur = head; cout<<"ENTER IC NUMBER :"; //enter ic no. of customer you search for. cin>>ic; while(cur != NULL && ic != cur->ic) { cur = cur->next; //cur move to next nod. } if(cur == NULL) cout<<"SORRY, NO DATA FOUND IN THE LIST:-"; else cout<<"DATA FOUND :- "; } //function to display data in the list. void display() { abc *cur; //declare cur pointer cur = head ; //new value inserted (cur) is equal to value of head while(cur != NULL) //while current is not NULL, the loop will print value { cout<<"\nNAME :"<<cur->name<<endl; //output cur = cur->next; //new value inserted will place at front of value inserted before. cout<<"IC NUMBER :"<<cur->ic<<endl; cur = cur->next; cout<<"DATE OF BIRTH :"<<cur->dte_b<<endl; cur = cur->next; } } [/CODE] Quote Share this post Link to post Share on other sites
NeoLogy 74 Report post Posted November 27, 2012 awak tak initialize variable respond. bile gune while loop..condition pada while always false..so, dia tidak akan masuk dalam while loop untuk check input kepada respond dan terus keluar dari while loop. Quote Share this post Link to post Share on other sites
CFoo++ 4 Report post Posted November 27, 2012 Betul3. Must be initilized to 'Y'. char respond = 'Y'; Quote Share this post Link to post Share on other sites
Chuki2 32 Report post Posted November 27, 2012 (edited) [CODE] while(respond == 'Y' || respond == 'y') [/CODE] Kesilapan pertama, while ada syarat terlebih dahulu.... Jadi cara lain maybe leh wat cam gini [CODE] char respond = 'Y'; while(respond == 'Y' || respond == 'y') { bla bla bla.... } [/CODE] Happy Programming.... Edited November 27, 2012 by Chuki2 Quote Share this post Link to post Share on other sites
daisygirl 0 Report post Posted November 27, 2012 terima kasih sudi membantu Quote Share this post Link to post Share on other sites