I have this assignment in C programing: Programming assignment (100 pts): Write
ID: 664706 • Letter: I
Question
I have this assignment in C programing:
Programming assignment (100 pts): Write a program that uses an array of structures to hold contact information for your friends. The program should allow the user to enter as many friends as the user wants. Create functions to add or delete entries in the phone book and to print valid phone book entries. Do not display phone book entries that are invalid or NULL (0). You can assume that all people have unique names. Make sure you allocate and free memory as necessary.
Output example:
Phone Book Application 1) Add friend
2) Delete friend
3) Show phone book
What do you want to do: 1
First name: Bob
Last name: Smith
Phone number: 123-4567
Record added to the phone book
I submitted something that didn't work, my instructor corrected it. This is the code I got back from my instructor:
#include
#include
#include
#include
#include
typedef struct _person
{
char fname[35];
char lname[35];
long int mble_no;
}person;
void menu();
void got();
void start();
void back();
person* addrecord(person*);
void listrecord(person*);
void modifyrecord();
person* deleterecord(person*);
void searchrecord(person*);
int count=0;
int main()
{
system("color 5f");
start();
return 0;
}
void back()
{
start();
}
void start()
{
menu();
}
void menu()
{
int selection = 0;
person* book = (person*)malloc(sizeof(person));
while(selection != 5){
//system("cls");
printf(" WELCOME TO Greg's PHONEBOOK");
printf(" MENU ");
printf(" 1.Add New 2.List 3.Delete 4.Search 5.Exit ");
scanf("%d",&selection);
switch(selection)
{
case 1:
book = addrecord(book);
break;
case 2:
listrecord(book);
break;
case 3:
book = deleterecord(book);
break;
case 4:
searchrecord(book);
break;
case 5:
exit(0);
break;
default:
system("cls");
printf(" Enter 1 to 4 only");
printf(" Enter any key");
}
}
}
person* addrecord(person* book)
{
person* tmp = (person*)realloc(book,(count+1)*sizeof(person));
if(tmp == NULL){
printf("Out of memory ");
return(book);
}
book = tmp;
printf(" First name: ");
scanf("%s", book[count].fname);
printf("Last name: ");
scanf("%s", book[count].lname);
printf("Phone number: ");
scanf("%d", &book[count].mble_no);
count++;
return(book);
}
void listrecord(person* book)
{
int i;
for(i=0; i printf("%s %s %d ",book[i].fname,book[i].lname,book[i].mble_no);
}
}
person* deleterecord(person* book){
int i,j;
char lname[35];
char fname[35];
printf("Enter the first name to delete ");
scanf("%s",fname);
printf("Enter the last name to delete ");
scanf("%s",lname);
for(i=0; i if((strcmp(book[i].fname,fname) == 0)&& (strcmp(book[i].lname,lname) == 0)){
for(j=i+1; j
strcpy(book[j-1].fname,book[j].fname);
strcpy(book[j-1].lname,book[j].lname);
book[j-1].mble_no = book[j].mble_no;
}
printf("Record has been deleted ");
count--;
if(count != 0)
book = (person*)realloc(book,(count)*sizeof(person));
return(book);
}
}
printf("Record could not be found ");
return(book);
}
void searchrecord(person* book){
int i;
char lname[35];
char fname[35];
printf("Enter the first name to search ");
scanf("%s",fname);
printf("Enter the last name to search ");
scanf("%s",lname);
for(i=0; i if((strcmp(book[i].fname,fname) == 0)&& (strcmp(book[i].lname,lname) == 0)){
printf("Record: %s %s %d ",book[i].fname,book[i].lname,book[i].mble_no);
break;
}
}
if(i== count)
printf("Record could not be found ");
}
/*
void searchrecord()
{
struct person p;
FILE *f;
char name[100];
f=fopen("project","rb");
if(f==NULL)
{
printf(" error in opening");
exit(1);
}
printf(" Enter name of person to search ");
got(name);
while(fread(&p,sizeof(p),1,f)==1)
{
if(strcmp(p.name,name)==0)
{
printf(" Detail Information About %s",name);
printf(" Name:%s Mobile no:%ld ",p.name,p.mble_no);
}
else
printf("file not found");
}
fclose(f);
printf(" Enter any key");
getch();
system("cls");
menu();
}
*/
/*
void modifyrecord()
{
int c;
FILE *f;
int flag=0;
struct person p,s;
char name[50];
f=fopen("project","rb+");
if(f==NULL)
{
printf("CONTACT'S DATA NOT ADDED YET.");
exit(1);
}
else
{
system("cls");
printf(" Enter CONTACT'S NAME TO MODIFY: ");
got(name);
while(fread(&p,sizeof(p),1,f)==1)
{
if(strcmp(name,p.name)==0)
{
printf(" Enter name:");
got(s.name);
printf(" Enter phone no:");
scanf("%ld",&s.mble_no);
fseek(f,-sizeof(p),SEEK_CUR);
fwrite(&s,sizeof(p),1,f);
flag=1;
break;
}
fflush(stdin);
}
if(flag==1)
{
printf(" your data id modified");
}
else
{
printf(" data is not found");
}
fclose(f);
}
printf(" Enter any key");
getch();
system("cls");
menu();
}
*/
/*
void got(char *name)
{
int i=0,j;
char c,ch;
do
{
c=getch();
if(c!=8&&c!=13)
{
*(name+i)=c;
putch(c);
i++;
}
if(c==8)
{
if(i>0)
{
i--;
}
// printf("h");
system("cls");
for(j=0;j {
ch=*(name+j);
putch(ch);
}
}
}while(c!=13);
*(name+i)='';
}
*/
The new assignment is to modify this code to do the following:
1) Alphabetically sort the list of entries by name (first or last).
2) Find a phone number for a given name.
3) Randomly select a friend from the phonebook for you to call.
4) Delete everyone from the phonebook at the same time.
Can someone make the modifications and post by 5 pm 7/10/15?