Jump to content
Sign in to follow this  
daisygirl

Menu Menggunakan Struktur Data.

Recommended Posts

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. :D

[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 by Chuki2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...