please help im really out of ideas. this program is a phonebook that can store unlimeted mobile numbers, landline numbers and email addresses, this program also deletes, edits,inserts and sorts and display all the records.
this is what i've got so far:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
typedef char str1[50];
typedef char str2[20];
typedef struct M
{
str2 mNum;
struct M *next;
}mType, *mList;
typedef struct L
{
str2 lNum;
struct L *next;
}lType, *lList;
typedef struct E
{
str1 eAdd;
struct E *next;
}eType, *eList;
typedef struct P
{
str1 name;
mList mobile;
lList landline;
eList email;
struct P *next;
}pType, *phone;
void menu();
void Insert(phone *);
void Delete(phone *);
void Edit (phone *);
void Search (phone);
void Display(phone);
void main(void)
{
phone pHead=NULL;
char code;
clrscr();
do
{
clrscr();
printf("=====MENU=====");
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Edit");
printf("\n4.Search");
printf("\n5.Display");
printf("\n6.Exit");
printf("\n\nEnter Choice:");
scanf(" %d",&code);
clrscr();
switch(code)
{
case 1:Insert(&pHead);break;
case 2:Delete(&pHead);break;
case 3:Edit (&pHead);break;
case 4:Search(pHead);break;
case 5:Display(pHead);break;
case 6:printf("Goodbye");exit(1);break;
}
}while(code!=6);
}
/*============================DELETE==========================*/
void Delete (phone *A)
{
phone temp;
mList mob=NULL;
lList lline=NULL;
eList mail=NULL;
str1 N;
printf("Enter Contact You Want to Delete: ");
fflush (stdin); gets(N);
for(;(*A)!=NULL && strcmp((*A)->name, N)!=0;A=&(*A)->next);
if(strcmp((*A)->name, N)!=0)
{
printf("No Record Found");
}
else
{
temp=*A;
for(mob=temp->mobile;mob!=NULL;mob=temp->mobile)
{
temp->mobile=mob->next;
free(mob);
}
for(lline=temp->landline;lline!=NULL;lline=temp->landline)
{
temp->landline=lline->next;
free(lline);
}
for(mail=temp->email;mail!=NULL;mail=temp->email)
{
temp->email=mail->next;
free(mail);
}
*A=temp->next;
free(temp);
}
}
/*===========================INSERT==============================*/
void Insert (phone *A)
{
int code;
char ans,mob[20],land[20],mail[20],ncontact[100];
mList tempM; lList tempL; eList tempE;
phone info,*link;
printf("Enter Contact Name: ");
fflush(stdin);
gets(ncontact);
info=(struct P*)malloc(sizeof(struct P));
strcpy(info->name,ncontact);
info->next=NULL;
if(*A==NULL)
*A=info;
else
{
for(link=A; (stricmp(info->name,(*link)->name)>0)&&*link!=NULL; link=&(*link)->next);
info->next=*link;
*link=info;
}
do
{
clrscr();
printf("****Contact Info****");
printf("\n1.Mobile\n2.Landline\n3.Email\n4.Exit\n");
printf("Please enter your choice %s: ",ncontact);
scanf(" %d",&code);
switch(code)
{
case 1: printf("\nEnter mobile: ");
flushall();
gets(mob);
info->mobile=(mList)malloc(sizeof(mType *));
strcpy(info->mobile->mNum,mob);
info->mobile->next=NULL;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
while(ans!='N')
{ printf("\nEnter mobile: ");
flushall();
gets(mob);
tempM=(struct M*)malloc(sizeof(struct M));
strcpy(tempM->mNum,mob);
tempM->next=info->mobile;
info->mobile=tempM;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
}
break;
case 2:printf("\nEnter LandLine Number: ");
flushall();gets(land);
info->landline=(lList)malloc(sizeof(lList));
strcpy(info->landline->lNum,land);
info->landline->next=NULL;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
while(ans!='N')
{
printf("\nEnter LandLine Number: ");
flushall();gets(land);
tempL=(struct L*)malloc(sizeof(struct L));
strcpy(tempL->lNum,land);
info->landline=tempL;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
}
break;
case 3:printf("\nEnter Email Address: ");
flushall();gets(mail);
info->email=(eList)malloc(sizeof(eList));
strcpy(info->email->eAdd,mail);
info->email->next=NULL;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
while(ans!='N')
{
printf("\nEnter Email Address: ");
flushall();gets(mail);
tempE=(struct E*)malloc(sizeof(struct E));
strcpy(tempE->eAdd,land);
info->email=tempE;
printf("\nAdd More?[y/n]: ");
scanf(" %c",&ans);
ans=toupper(ans);
}
break;
}
}while(code!=4);
if((*A)!=NULL)
{
for(;(*A)!=NULL;A=&(*A)->next);
}
(*A)=info;
getch();
}
/*==========================EDIT============================*/
void Edit(phone *A)
{
str1 N;
int cnt,ctr;
phone bk;
mList *tempM; lList *tempL; eList *tempE;
printf("Enter Contact Name to be Edited: ");
fflush(stdin);gets(N);
for(;(*A)!=NULL && strcmp((*A)->name,N)!=0;A=&(*A)->next);
bk=*A;
do
{
clrscr();
printf("****Contact Info****");
printf("\n1.Mobile\n2.Landline\n3.Email\n4.Exit\n");
printf("Please enter your choice: ");
scanf(" %d",&ctr);
switch(ctr)
{
case 1:printf("Mobile Number(s):\n");
if(bk->mobile->mNum!=NULL)
{
for(cnt=1,tempM=&bk->mobile;(*tempM)!=NULL;cnt++,tempM=&(*tempM)->next)
{
printf("%d.)%s",cnt,(*tempM)->mNum);
}
printf("\nWhich one do you wish to edit? ");
scanf(" %d",&cnt);
for(tempM=&bk->mobile;cnt>1;cnt--,tempM=&(*tempM)->next);
printf("\n%s\nEnter New Number: ",(*tempM)->mNum);
fflush(stdin);gets((*tempM)->mNum);
printf("\n\n\nEditing Successful!!");
}
else
{
printf("No record found");getch();
}
break;
case 2:printf("Landline Number(s):\n");
if(bk->landline!=NULL)
{
for(cnt=1,tempL=&bk->landline;(*tempL)!=NULL;cnt++,tempL=&(*tempL)->next)
{
printf("%d.)%s",cnt,(*tempL)->lNum);
}
printf("\nWhich one do you wish to edit? ");
scanf(" %d",&cnt);
for(tempL=&bk->landline;cnt>1;cnt--,tempL=&(*tempL)->next);
printf("\n%s\nEnter New Number: ",(*tempL)->lNum);
fflush(stdin);gets((*tempL)->lNum);
printf("\n\n\nEditing Successful!!");
}
else
{
printf("No record found");getch();
}
break;
case 3:printf("Email Address(es):\n");
if(bk->email!=NULL)
{
for(cnt=1,tempE=&bk->email;(*tempE)!=NULL;cnt++,tempE=&(*tempE)->next)
{
printf("%d.)%s",cnt,(*tempE)->eAdd);
}
printf("\nWhich one do you wish to edit? ");
scanf(" %d",&cnt);
for(tempE=&bk->email;cnt>1;cnt--,tempE=&(*tempE)->next);
printf("\n%s\nEnter New Number: ",(*tempE)->eAdd);
fflush(stdin);gets((*tempE)->eAdd);
printf("\n\n\nEditing Successful!!");
}
else
{
printf("No record found");getch();
}
break;
}
}while(ctr!=4);
}
/*===========================SEARCH===========================*/
void Search(phone A)
{
str1 nme;
mList mobile; lList land; eList email;
int ctr=1;
clrscr();
printf("Enter Contact Name: ");
fflush(stdin);gets(nme);
for(;A!=NULL && strcmp(A->name,nme)!=0;A=A->next);
if(A!=NULL)
{
clrscr();
printf("Contact Name: %s", A->name);
printf("\n****Contact Info****");
printf("\n\nMobile Number(s)");
for(mobile=A->mobile;mobile!=NULL;mobile=mobile->next)
{
printf("\n%d.)%s",ctr,mobile->mNum); ctr++;
}
printf("\nLandlinee Number(s)");
for(land=A->landline;land!=NULL;land=land->next)
{
printf("\n%d.)%s",ctr,land->lNum);ctr++;
}
printf("\nEmail Address(es)");
for(email=A->email;email!=NULL;email=email->next)
{
printf("\n%d.)%s",ctr,email->eAdd);ctr++;
}
}
else
{
printf("Contact doe not exist!");
}
getch();
}
/*===========================DISPLAY=========================*/
void Display(phone B)
{
int ctr;
mList tempM;
lList tempL;
eList tempE;
if(B!=NULL)
for(ctr=1;B!=NULL;B=B->next,ctr++)
{
clrscr();
printf("CONTACT INFO #%d",ctr);
printf("\n\nNAME: %s",B->name);
printf("\n\nMOBILE NUMBER(S)");
for(tempM=B->mobile;tempM!=NULL;tempM=tempM->next)
{
printf("\n%s",tempM->mNum);
}
printf("\n\nLANDLINE NUMBER(S)");
for(tempL=B->landline;tempL!=NULL;tempL=tempL->next)
{
printf("\n%s",tempL->lNum);
}
printf("\n\nEMAIL ADDRESS(ES)");
for(tempE=B->email;tempE!=NULL;tempE=tempE->next)
{
printf("\n%s",tempE->eAdd);
}
}
else
{
printf("THERE IS NO EXISTING RECORD");getch();
}
}