i have this program and i'm already done with the data entry, search, display and exit menu.. my only problem is my search and delete and the search and edit...please help me. i've attached my unfinished program here:
Programming Assignment:
Write a program to simulate a telephone directory. The user should be able choose between the following modules:
a. Data Entry
b. Search for a Record
c. Search and Edit a Record
d. Search and Delete a Record
e. Display all Records
f. Exit from the program
- Create your own structure for the telephone directory (choose your fields). Limit your fields to 5.
- Search a record by letting the user enter the last name of the person in the directory. The user should be able to search for a name in any case (lower- or uppercase). This means that BAUTISTA and Bautista are the same. If there are two or more records with the same last name, show all of them.
- When deleting records in the first or middle locations, the components in the array should be moved backwards.
- Display all the records in tabular form
my unfinished program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
/*void addrec(directory dir[10]); */
struct directory
{
char last[20];
char first[20];
char add[50];
char mobile[20];
char tphone[20];
};
typedef struct directory directory;
directory dir[10];
int addrec(directory dir[]);
void display(directory dir[], int y);
void search(directory dir[], int y);
int delete(directory dir[], int y);
main()
{
int i, y;
directory dir[10];
char ans, choice;
do{
clrscr();
printf(" MENU ");
printf("\n[A] ADD RECORD");
printf("\n[B] SEARCH RECORD");
printf("\n[C] EDIT RECORD");
printf("\n[D] DELETE RECORD");
printf("\n[E] DISPLAY RECORD");
printf("\n[F] EXIT");
printf("\nEnter choice: ");
scanf("%s", &ans);
switch(ans)
{
case 'A':
case 'a': clrscr();
do{
y=addrec(dir);
clrscr();
printf("Another?");
scanf("%c", &choice);
}while((choice=='y')&&(y!=10));
break;
case 'B':
case 'b': clrscr();
search(dir, y);
break;
case 'D':
case 'd': clrscr();
delete(dir, y);
break;
case 'E':
case 'e': clrscr();
display(dir, y);
break;
case 'f':
case 'F': clrscr();
printf("Program Terminated. press any key.");
getch();
}
}while((ans!='f')&&(ans!='F'));
}
int addrec(directory dir[])
{
static int i=0;
char last[20], first[20], add[50], mobile[20], tphone[20];
printf("Enter the following data:");
gets("\n");
printf("\nLast Name: ");
gets(last);
strcpy(dir[i].last, last);
printf("First Name: ");
gets(first);
strcpy(dir[i].first, first);
printf("Address: ");
gets(add);
strcpy(dir[i].add, add);
printf("Mobile number: ");
gets(mobile);
strcpy(dir[i].mobile, mobile);
printf("Telephone number: ");
gets(tphone);
strcpy(dir[i].tphone, tphone);
i++;
return i;
}
void display(directory dir[], int y)
{
int i;
clrscr();
printf("LAST NAME\tFIRST NAME\tADDRESS\t\tMOBILE\t\tTELEPHONE\n\n");
for(i=0; i<y; i++)
{
printf("\n%s\t\t%s", dir[i].last, dir[i].first);
printf("\t\t%s", dir[i].add);
printf("\t\t%s\t\t%s", dir[i].mobile, dir[i].tphone);
}
getch();
/* printf("Nothing to be displayed.");*/
}
void search(directory dir[], int y)
{
char slast[20];
int i=0;
printf("SEARCH\n\n");
printf("Enter lastname: ");
scanf("%s", &slast);
printf("\nResult(s) for %s: ", slast);
printf("\n\nLAST NAME\tFIRST NAME\tADDRESS\t\tMOBILE\t\tTELEPHONE\n\n");
for(i=0; i<y; i++)
{
if(strcmpi(slast, dir[i].last)==0)
{
printf("\n%s\t\t%s", dir[i].last, dir[i].first);
printf("\t\t%s", dir[i].add);
printf("\t\t%s\t\t%s", dir[i].mobile, dir[i].tphone);
}
}
getch();
}
int delete(directory dir[], int y)
{
char dlast[20], del[20];
int i;
printf("DELETE");
printf("\n\n\n");
printf("Enter to search lastname: ");
scanf("%s", &dlast);
printf("\nResult(s) for %s: ", dlast);
printf("\n\nLAST NAME\tFIRST NAME\tADDRESS\t\tMOBILE\t\tTELEPHONE\n\n");
for(i=0; i<y; i++)
{
if(strcmpi(dlast, dir[i].last)==0)
{
printf("\n%s\t\t%s", dir[i].last, dir[i].first);
printf("\t\t%s", dir[i].add);
printf("\t\t%s\t\t%s", dir[i].mobile, dir[i].tphone);
}
}
printf("\n\nPlease enter the first name of the person you want to delete: ");
scanf("%s", &del);
for(i=0; i<y; i++)
{
if(strcmpi(del, dir[i].first)==0)
{
/* strcpy(dir[i].last,NULL);
strcpy(dir[i].first,NULL);
strcpy(dir[i].add,NULL);
strcpy(dir[i].mobile,NULL);
strcpy(dir[i].tphone,NULL);
*/
strcpy(dir[i++].last, dir[i].last);
strcpy(dir[i++].first, dir[i].first);
strcpy(dir[i++].add, dir[i].add);
strcpy(dir[i++].mobile, dir[i].mobile);
strcpy(dir[i++].tphone, dir[i].tphone);
strcpy(dir[i+2].last, NULL);
strcpy(dir[i+2].first, NULL);
strcpy(dir[i+2].add, NULL);
strcpy(dir[i+2].mobile, NULL);
strcpy(dir[i+2].tphone, NULL);
/* y--; */
}
/* y--; */
}
printf("\nDeletion successful.");
printf("\npress any key...");
getch();
return(y);
}