phonebook help

This is a discussion on phonebook help within the C Programming forums, part of the General Programming Boards category; hey guys, I have written this code for a phonebook, I wanted to know what to do, so that people ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    phonebook help

    hey guys, I have written this code for a phonebook, I wanted to know what to do, so that people can search for others by their phone number using linear search.

    Code:
    /***********************************************************************
    Description:This is an interactive menu driven program. It is designed
    to implement an automated telephone book. Each entry in this automated
    telephone book will contain a name and a telephone number.
    ***********************************************************************/
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define TEL_BOOK_SIZE 75
    #define NAME_SIZE 22
    #define TEL_NUM_SIZE 14
    
    struct tel_book_element
    {	char name[NAME_SIZE];
    	char tel_num[TEL_NUM_SIZE];
    };
    
    typedef struct tel_book_element Tel_Book_Element;
    
    struct tel_book
    {	Tel_Book_Element entry[TEL_BOOK_SIZE];
    	int n;
    };
    
    typedef struct tel_book Tel_Book;
    
    int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip);
    void putTelBook(const char *fname,Tel_Book *tbp);
    void getTelBook(const char *fname,Tel_Book *tbp);
    void displayMenu(void);
    char *strToUpper(char *s);
    void lookUpName(Tel_Book *tbp);
    void listName(Tel_Book *tbp);
    void modify(Tel_Book *tbp);
    void del(Tel_Book *tbp);
    void inputNewBook(char *fname,Tel_Book *tbp);
    void sortTelBook(Tel_Book *tbp);
    
    int main()
    {  Tel_Book tb;
    	char save,keyHit,fname[NAME_SIZE]="a:tbfile.txt";
    
    	getTelBook(fname,&tb);
    	do
    	{  displayMenu();
    		printf("Enter Your Selection (a-f) ");
    		keyHit=getchar();
    		getchar();      /* get rid of the new line character */
    		switch(keyHit)
    		{	case'a':case'A': lookUpName(&tb); break;
    			case'b':case'B': listName(&tb); break;
    			case'c':case'C': modify(&tb); break;
    			case'd':case'D': del(&tb); break;
    			case'e':case'E': inputNewBook(fname,&tb); break;
    			case'f':case'F':
    				printf("Do you want to save it (y/n)? "); save=getchar();
    				if(save=='y'||save=='Y')
    					putTelBook(fname,&tb);
    				break;
    			default:
    				printf("Option is not correct. Hit Enter to continue");
    				getchar();
    				break;
    		}
    	}while(keyHit!='f' && keyHit!='F');
    
    	return 0;
    }
    
    /***********************************************************************
    The function inputNewBook close the current TelBook and read the new
    TelBook from file, uppercase name, and sort in alphabetical order.
    ***********************************************************************/
    void inputNewBook(char *fname,Tel_Book *tbp)
    {  int m;
    	char save;
    
    	printf("Do you want to save the current one (y/n)? ");
    	save=getchar(); getchar();
    	if(save=='y'||save=='Y')
    		putTelBook(fname,tbp);
    	printf("Enter the filename: ");
    	gets(fname);
    	getTelBook(fname,tbp);
    	for(m=0;m<tbp->n;m++)
    		strToUpper(tbp->entry[m].name);
    	sortTelBook(tbp);
    	printf("New telephone book has been inputed.\n");
    	printf("Press Enter to continue");         /* pause the output */
    	getchar();
    }
    
    /***********************************************************************
    The function sortTelBook sort all the name in alphabetical order.
    ***********************************************************************/
    void sortTelBook(Tel_Book *tbp)
    {  int pass,m;
    	char hold[NAME_SIZE];
    
    	for(pass=1;pass<tbp->n;pass++)
    		for(m=0;m<(tbp->n)-pass;m++)
    			if(strcmp(tbp->entry[m].name,tbp->entry[m+1].name)>0)
    			{	/* swap 2 elements */
    				strcpy(hold,tbp->entry[m].name);
    				strcpy(tbp->entry[m].name,tbp->entry[m+1].name);
    				strcpy(tbp->entry[m+1].name,hold);
    				strcpy(hold,tbp->entry[m].tel_num);
    				strcpy(tbp->entry[m].tel_num,tbp->entry[m+1].tel_num);
    				strcpy(tbp->entry[m+1].tel_num,hold);
    			}
    }
    
    /***********************************************************************
    This function displays the options of the interactive-user program.
    ***********************************************************************/
    void displayMenu(void)
    {
    	
    	printf("a) Look-up Name\n");
    	printf("b) List Names Starting With Given String\n");
    	printf("c) Insert New or Modified Entry\n");
    	printf("d) Delete Entry\n");
    	printf("e) Input New Telephone Book\n");
    	printf("f) Quit\n");
    }
    
    /***********************************************************************
    This function changes a string to uppercase.
    ***********************************************************************/
    char *strToUpper(char *s)
    {	int i;
    
    	for(i=0;s[i]!='\0';i++)
    		s[i]=toupper(s[i]);
    	return s;
    }
    
    /***********************************************************************
    This function search a name, return 1 if found, 0 if not found, and ip
    is the position found or should be inserted.
    ***********************************************************************/
    int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip)
    {	int low,high,middle;
    
    	low=0;high=tbp->n-1;
    	while(low<=high)
    	{	middle=(low+high)/2;
    		if(strcmp(searchName,tbp->entry[middle].name)==0)
    		{	*ip=middle;
    			return 1;
    		}
    		else if(strcmp(searchName,tbp->entry[middle].name)<0)
    			high=middle-1;
    		else
    			low=middle+1;
    	}
    	*ip=low;
    	return 0;
    }
    
    /***********************************************************************
    This function read information from a file, and store it to a struct
    typed TelBook.
    ***********************************************************************/
    void getTelBook(const char *fname,Tel_Book *tbp)
    {  FILE *fp;
    	int m=0;
    
    	if ((fp=fopen(fname, "r"))==NULL)
    	{	printf("File could not be opened to read\n");
    		exit(1);   /* terminate the program  */
    	}
    	while(fgets(tbp->entry[m].name,NAME_SIZE,fp)!=NULL)
    	/* read while not EOF */
    		fgets(tbp->entry[m++].tel_num,TEL_NUM_SIZE,fp);
    	tbp->n=m;      /* store the total of entries */
    	fclose(fp);
    }
    
    /***********************************************************************
    This function output information from a struct TelBook to a file.
    ***********************************************************************/
    void putTelBook(const char *fname,Tel_Book *tbp)
    { 	int m;
    	FILE *fp;
    
    	if ((fp=fopen(fname, "w"))==NULL)
    	{	printf("File could not be opened to write\n");  exit(1);
    	}
    	for(m=0;m<tbp->n;m++)
    	{	fputs(tbp->entry[m].name,fp);
    		fputs(tbp->entry[m].tel_num,fp);
    	}
    	fclose(fp);
    }
    
    /***********************************************************************
    This function provides an option that user can look up an entry of the
    telephone book.
    ***********************************************************************/
    void lookUpName(Tel_Book *tbp)
    {	char searchName[NAME_SIZE];
    	int i;
    
    	printf("Enter a name you want you look up: ");
    	fgets(searchName,NAME_SIZE,stdin);
    	strToUpper(searchName);
    	if(binarySearch(tbp,searchName,&i))
    	{	printf("Name found:\n");
    		printf("%s",tbp->entry[i].name);
    		printf("%s",tbp->entry[i].tel_num);
    	}
    	else
    		printf("Name not Found\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    This function provides an option that user can list all the names which
    begin with the first name inputted.
    ***********************************************************************/
    void listName(Tel_Book *tbp)
    {	char name[NAME_SIZE];
    	int i,found=0;
    
    	printf("Enter a name you want to list: ");
    	gets(name);
    	strToUpper(name);
    	for(i=0;i<tbp->n;i++)
    		if(tbp->entry[i].name==strstr(tbp->entry[i].name,name))
    		{  found=1;
    			printf("%s",tbp->entry[i].name);
    			printf("%s",tbp->entry[i].tel_num);
    		}
    	if (!found)
    		printf("Name not in the list\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    This function provides an option that user can input an entry, if found
    then modify, if not found then insert as a new entry.
    ***********************************************************************/
    void modify(Tel_Book *tbp)
    {  int i,j;
    	char name[NAME_SIZE],tel[TEL_NUM_SIZE];
    
    	printf("Enter the name to modify or insert: ");
    	fgets(name,NAME_SIZE,stdin);
    	strToUpper(name);
    	printf("Enter the telephone number: ");
    	fgets(tel,TEL_NUM_SIZE,stdin);
    	if(binarySearch(tbp,name,&i))             /* found entry */
    	{	printf("Modified Entry Inserted\n");
    		strcpy(tbp->entry[i].name,name);    /* i is position modified */
    		strcpy(tbp->entry[i].tel_num,tel);
    	}
    	else           /* not found the entry, i is position inserted */
    	{
    		printf("New Entry Inserted\n");
    /*		moving entries up by one position*/
    		for(j=tbp->n;j>i;j--)
    		{	strcpy(tbp->entry[j].name,tbp->entry[j-1].name);
    			strcpy(tbp->entry[j].tel_num,tbp->entry[j-1].tel_num);
    		}
    		strcpy(tbp->entry[i].name,name);
    		strcpy(tbp->entry[i].tel_num,tel);
    		tbp->n++;
    	}
    	printf("Press Enter to continue");
    	getchar();
    
    }
    
    /***********************************************************************
    This function provides an option that user can delete one entry.
    ***********************************************************************/
    void del(Tel_Book *tbp)
    {  int i,j;
    	char name[NAME_SIZE];
    
    	printf("Enter the name to delete: ");
    	fgets(name,NAME_SIZE,stdin);
    	strToUpper(name);
    	if(binarySearch(tbp,name,&i))
    	{	printf("Entry Deleted\n");
    		for(j=i;j<tbp->n-1;j++)   /*	moving entries down by one position*/
    		{	strcpy(tbp->entry[j].name,tbp->entry[j+1].name);
    			strcpy(tbp->entry[j].tel_num,tbp->entry[j+1].tel_num);
    		}
    		tbp->n--;
    	}
    	else
    		printf("Name not Found\n");
    	printf("Press Enter to continue");
    	getchar();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    hey guys, I have written this code for a phonebook, I wanted to know what to do, so that people can search for others by their phone number using linear search.

    Ok, and? Where's your problem? We're not going to all copy and paste your code in and compile it, make our own data file to read, etc.

    Where's your problem, and what do you need help with?

    On a side note, thanks for actually using code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    21
    use a binary search

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    i have added the linear search to the program, but for some reason it always says number not found,
    here is the code
    Code:
    /***********************************************************************
    Description:This is an interactive menu driven program. It is designed
    to implement an automated telephone book. Each entry in this automated
    telephone book will contain a name and a telephone number.
    ***********************************************************************/
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define TEL_BOOK_SIZE 75
    #define NAME_SIZE 22
    #define TEL_NUM_SIZE 14
    
    struct tel_book_element
    {	char name[NAME_SIZE];
    	char tel_num[TEL_NUM_SIZE];
    };
    
    typedef struct tel_book_element Tel_Book_Element;
    
    struct tel_book
    {	Tel_Book_Element entry[TEL_BOOK_SIZE];
    	int n;
    };
    
    typedef struct tel_book Tel_Book;
    
    int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip);
    int linearSearch(const Tel_Book *tbp, const char num[]);
    void putTelBook(const char *fname,Tel_Book *tbp);
    void getTelBook(const char *fname,Tel_Book *tbp);
    void displayMenu(void);
    char *strToUpper(char *s);
    void lookUpName(Tel_Book *tbp);
    void listName(Tel_Book *tbp);
    void modify(Tel_Book *tbp);
    void del(Tel_Book *tbp);
    void inputNewBook(char *fname,Tel_Book *tbp);
    void sortTelBook(Tel_Book *tbp);
    void lookupnumber(Tel_Book *tbp);
    
    int main()
    {  Tel_Book tb;
    	char save,keyHit,fname[NAME_SIZE]="a:tbfile.txt";
    
    	getTelBook(fname,&tb);
    	do
    	{  displayMenu();
    		printf("Enter Your Selection (a-f) ");
    		keyHit=getchar();
    		getchar();      /* get rid of the new line character */
    		switch(keyHit)
    		{	case '1': inputNewBook(fname,&tb); break;
    		    case '2': lookUpName(&tb); break;
    			case '4': modify(&tb); break;
    			case '5': del(&tb); break;
    			case '6': listName(&tb); break;
    			case '3': lookupnumber(&tb);break;
    			case '7':
    				printf("Do you want to save it (y/n)? "); save=getchar();
    				if(save=='y'||save=='Y')
    					putTelBook(fname,&tb);
    				break;
    			default:
    				printf("Option is not correct. Hit Enter to continue");
    				getchar();
    				break;
    		}
    	}while(keyHit!='7');
    
    	return 0;
    }
    
    /***********************************************************************
    The function inputNewBook close the current TelBook and read the new
    TelBook from file, uppercase name, and sort in alphabetical order.
    ***********************************************************************/
    void inputNewBook(char *fname,Tel_Book *tbp)
    {  int m;
    	char save;
    
    	printf("Do you want to save the current one (y/n)? ");
    	save=getchar(); getchar();
    	if(save=='y'||save=='Y')
    		putTelBook(fname,tbp);
    	printf("Enter the filename: ");
    	gets(fname);
    	getTelBook(fname,tbp);
    	for(m=0;m<tbp->n;m++)
    		strToUpper(tbp->entry[m].name);
    	sortTelBook(tbp);
    	printf("New telephone book has been inputed.\n");
    	printf("Press Enter to continue");         /* pause the output */
    	getchar();
    }
    
    /***********************************************************************
    The function sortTelBook sort all the name in alphabetical order.
    ***********************************************************************/
    void sortTelBook(Tel_Book *tbp)
    {  int pass,m;
    	char hold[NAME_SIZE];
    
    	for(pass=1;pass<tbp->n;pass++)
    		for(m=0;m<(tbp->n)-pass;m++)
    			if(strcmp(tbp->entry[m].name,tbp->entry[m+1].name)>0)
    			{	/* swap 2 elements */
    				strcpy(hold,tbp->entry[m].name);
    				strcpy(tbp->entry[m].name,tbp->entry[m+1].name);
    				strcpy(tbp->entry[m+1].name,hold);
    				strcpy(hold,tbp->entry[m].tel_num);
    				strcpy(tbp->entry[m].tel_num,tbp->entry[m+1].tel_num);
    				strcpy(tbp->entry[m+1].tel_num,hold);
    			}
    }
    
    /***********************************************************************
    This function displays the options of the interactive-user program.
    ***********************************************************************/
    void displayMenu(void)
    {
      printf("Automated Telephone Book Menu\n");	
    	printf("1) New telephone book-upper case and sort it\n");
    	printf("2) Look up name to get number\n");
    	printf("3) Look up number to get name\n");
    	printf("4) Insert new/modified element\n");
    	printf("4) Delete element\n");
    	printf("6) List names starting with given letter\n");
    	printf("7) Quit\n");
    }
    
    /***********************************************************************
    This function changes a string to uppercase.
    ***********************************************************************/
    char *strToUpper(char *s)
    {	int i;
    
    	for(i=0;s[i]!='\0';i++)
    		s[i]=toupper(s[i]);
    	return s;
    }
    
    /***********************************************************************
    This function search a name, return 1 if found, 0 if not found, and ip
    is the position found or should be inserted.
    ***********************************************************************/
    int binarySearch(const Tel_Book *tbp, const char searchName[], int *ip)
    {	int low,high,middle;
    
    	low=0;high=tbp->n-1;
    	while(low<=high)
    	{	middle=(low+high)/2;
    		if(strcmp(searchName,tbp->entry[middle].name)==0)
    		{	*ip=middle;
    			return 1;
    		}
    		else if(strcmp(searchName,tbp->entry[middle].name)<0)
    			high=middle-1;
    		else
    			low=middle+1;
    	}
    	*ip=low;
    	return 0;
    }
    
    /***********************************************************************
    This function read information from a file, and store it to a struct
    typed TelBook.
    ***********************************************************************/
    void getTelBook(const char *fname,Tel_Book *tbp)
    {  FILE *fp;
    	int m=0;
    
    	if ((fp=fopen(fname, "r"))==NULL)
    	{	printf("File could not be opened to read\n");
    		exit(1);   /* terminate the program  */
    	}
    	while(fgets(tbp->entry[m].name,NAME_SIZE,fp)!=NULL)
    	/* read while not EOF */
    		fgets(tbp->entry[m++].tel_num,TEL_NUM_SIZE,fp);
    	tbp->n=m;      /* store the total of entries */
    	fclose(fp);
    }
    
    /***********************************************************************
    This function output information from a struct TelBook to a file.
    ***********************************************************************/
    void putTelBook(const char *fname,Tel_Book *tbp)
    { 	int m;
    	FILE *fp;
    
    	if ((fp=fopen(fname, "w"))==NULL)
    	{	printf("File could not be opened to write\n");  exit(1);
    	}
    	for(m=0;m<tbp->n;m++)
    	{	fputs(tbp->entry[m].name,fp);
    		fputs(tbp->entry[m].tel_num,fp);
    	}
    	fclose(fp);
    }
    
    /***********************************************************************
    This function provides an option that user can look up an entry of the
    telephone book.
    ***********************************************************************/
    void lookUpName(Tel_Book *tbp)
    {	char searchName[NAME_SIZE];
    	int i;
    
    	printf("Enter a name you want you look up: ");
    	fgets(searchName,NAME_SIZE,stdin);
    	strToUpper(searchName);
    	if(binarySearch(tbp,searchName,&i))
    	{	printf("Name found:\n");
    		printf("%s",tbp->entry[i].name);
    		printf("%s",tbp->entry[i].tel_num);
    	}
    	else
    		printf("Name not Found\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    This function provides an option that user can list all the names which
    begin with the first name inputted.
    ***********************************************************************/
    void listName(Tel_Book *tbp)
    {	char name[NAME_SIZE];
    	int i,found=0;
    
    	printf("Enter a name you want to list: ");
    	gets(name);
    	strToUpper(name);
    	for(i=0;i<tbp->n;i++)
    		if(tbp->entry[i].name==strstr(tbp->entry[i].name,name))
    		{  found=1;
    			printf("%s",tbp->entry[i].name);
    			printf("%s",tbp->entry[i].tel_num);
    		}
    	if (!found)
    		printf("Name not in the list\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    This function provides an option that user can input an entry, if found
    then modify, if not found then insert as a new entry.
    ***********************************************************************/
    void modify(Tel_Book *tbp)
    {  int i,j;
    	char name[NAME_SIZE],tel[TEL_NUM_SIZE];
    
    	printf("Enter the name to modify or insert: ");
    	fgets(name,NAME_SIZE,stdin);
    	strToUpper(name);
    	printf("Enter the telephone number: ");
    	fgets(tel,TEL_NUM_SIZE,stdin);
    	if(binarySearch(tbp,name,&i))             /* found entry */
    	{	printf("Modified Entry Inserted\n");
    		strcpy(tbp->entry[i].name,name);    /* i is position modified */
    		strcpy(tbp->entry[i].tel_num,tel);
    	}
    	else           /* not found the entry, i is position inserted */
    	{
    		printf("New Entry Inserted\n");
    /*		moving entries up by one position*/
    		for(j=tbp->n;j>i;j--)
    		{	strcpy(tbp->entry[j].name,tbp->entry[j-1].name);
    			strcpy(tbp->entry[j].tel_num,tbp->entry[j-1].tel_num);
    		}
    		strcpy(tbp->entry[i].name,name);
    		strcpy(tbp->entry[i].tel_num,tel);
    		tbp->n++;
    	}
    	printf("Press Enter to continue");
    	getchar();
    
    }
    
    /***********************************************************************
    This function provides an option that user can delete one entry.
    ***********************************************************************/
    void del(Tel_Book *tbp)
    {  int i,j;
    	char name[NAME_SIZE];
    
    	printf("Enter the name to delete: ");
    	fgets(name,NAME_SIZE,stdin);
    	strToUpper(name);
    	if(binarySearch(tbp,name,&i))
    	{	printf("Entry Deleted\n");
    		for(j=i;j<tbp->n-1;j++)   /*	moving entries down by one position*/
    		{	strcpy(tbp->entry[j].name,tbp->entry[j+1].name);
    			strcpy(tbp->entry[j].tel_num,tbp->entry[j+1].tel_num);
    		}
    		tbp->n--;
    	}
    	else
    		printf("Name not Found\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    This function looks up the number
    ***********************************************************************/
    
    void lookupnumber(Tel_Book *tbp)
    {
    	char num[TEL_NUM_SIZE];
    	int i=0,k;
    
    	printf("Enter a number to look up: ");
    	fgets(num,TEL_NUM_SIZE,stdin);
    	k=linearSearch(tbp,num);
    	if(k!=-1)
    	{
    		printf("Name found:\n");
    		printf("%s",tbp->entry[k].name);
    		printf("%s",tbp->entry[k].tel_num);
    	}
    	else
    	printf("Name not Found\n");
    	printf("Press Enter to continue");
    	getchar();
    }
    
    /***********************************************************************
    performs linear search
    ***********************************************************************/
    
    int linearSearch(const Tel_Book *tbp, const char num[])
    {
    	int n=0;
    
    	for(n=0;n<=13;n++)
    		if(tbp->entry[n].tel_num==num[n])
    			return n;
    
    		return -1;
    }

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    I didn't look through the code sorry but a good way to spot general errors like that is to narrow them down. What I do(and this may be considered "ghetto" I dunno) is throw printf statements in where I think its messing up. The printf can

    1. let you know if certain conditions are met and which route the program is going
    2. let you display variable values, and you can compare and make sure they changed the way they are expected to.
    3. narrow the problem down, like if you know its messing up right before functionX, and worked up until a certain point before that it helps alot in narrowing things down.

    There's prolly better ways to do this, like using a debugger maybe, I wouldn't know never used one yet. Anyways try that, goodluck.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    thanks alot for your help, now all i have to do is make everything dynamic allocation.
    http://kashif.pcplayground.com/phonebook.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  2. Linked list question....again
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 05:59 PM
  3. phonebook error
    By cguy in forum C Programming
    Replies: 2
    Last Post: 01-30-2003, 01:08 AM
  4. What am I doing wrong?
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-26-2003, 11:39 PM
  5. phonebook
    By jk81 in forum C Programming
    Replies: 6
    Last Post: 09-25-2002, 05:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21