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();
	}
}

You post 376 lines of code and don't tell us what the problem is?

oh sorry the search and display doesn't work and i couldn't check if the edit and the delete works until those two other functions are properly running. i've been debugging this for 3 days and i still don't see where i went wrong so im finally asking for help. so please if you can help please do. thank you

I looked at your code...well I tried to compile it and couldn't because you have too much reliance on conio.h which isn't available on my machine...You'll have to wait for a Windows programmer...

I looked at your code...well I tried to compile it and couldn't because you have too much reliance on conio.h which isn't available on my machine...You'll have to wait for a Windows programmer...

oh okay well thank you for your time, i hope my teacher's going to accept late projects hahaha

I got it to compile with VC++ 2008 Express with only a couple minor changes.

A couple obversations:

1) line 45: code should be declared as int, not char because the scanf() on line 58 expects a pointer to an int.

2) delete all lines that call fflush(stdin) because fflush() is only used to flush output streams, not input streams. Your compiler might support fflush(stdin) but your teacher's compiler may or may not. If your teacher can not compile your program that you will most likely get some marks knocked off your grade. Instead of fflush(stdin) you need to flush the keyboard buffer of '\n' whenever you ask for numeric data entry, such as scanf("%d" ...). One way to do it is like this:

int code;
char c;
printf("Enter something\n");
scanf("%d%c", &code, &c);

3) ditto for fflushall() == that will flush all output streams which you are not using except stdout.

4) Now to the good part. In both Insert() and Search() you are destroying the linked list by changing the linked list's pointer to the top node! Don't do that. The only time that pointer should be changed is if you need to insert a new node at the head of the linked list or delete it.

on line 113 change phone *link to phone link , removing the star. Than change line 130 to this: for(link=*A; link != NULL && stricmp(info->name,link->name)>0; link=link->next); delete line 132.


delete lines 213-218 because they are not necessary and also destroy to pointer to the linked list.
Similar changes need to be made to Search()

Edited 6 Years Ago by Ancient Dragon: n/a

I got it to compile with VC++ 2008 Express with only a couple minor changes.

A couple obversations:

1) line 45: code should be declared as int, not char because the scanf() on line 58 expects a pointer to an int.

2) delete all lines that call fflush(stdin) because fflush() is only used to flush output streams, not input streams. Your compiler might support fflush(stdin) but your teacher's compiler may or may not. If your teacher can not compile your program that you will most likely get some marks knocked off your grade. Instead of fflush(stdin) you need to flush the keyboard buffer of '\n' whenever you ask for numeric data entry, such as scanf("%d" ...). One way to do it is like this:

int code;
char c;
printf("Enter something\n");
scanf("%d%c", &code, &c);

3) ditto for fflushall() == that will flush all output streams which you are not using except stdout.

4) Now to the good part. In both Insert() and Search() you are destroying the linked list by changing the linked list's pointer to the top node! Don't do that. The only time that pointer should be changed is if you need to insert a new node at the head of the linked list or delete it.

on line 113 change phone *link to phone link , removing the star. Than change line 130 to this: for(link=*A; link != NULL && stricmp(info->name,link->name)>0; link=link->next); delete line 132.


delete lines 213-218 because they are not necessary and also destroy to pointer to the linked list.
Similar changes need to be made to Search()

thank you :D

Those are only a few of the millions of problems I see in your program. Well, millions is a bit of exaggeration :)

Other tips:
use int main not void main. main() always returns an int whether you code it to return one or not.

replace gets() with fgets() because gets() will allow you to enter more characters than the input buffer can hold.

Recommendation---

Start over.
Write the input function. Test it.
Write the display function. Test it.

you can copy the code from the current code to make it faster, but from now on *never* write the entire project without compiling and testing often. Write it in segments.

This article has been dead for over six months. Start a new discussion instead.