In c/c++ programming Add additional functionality to your phonebook program from
ID: 3600896 • Letter: I
Question
In c/c++ programming Add additional functionality to your phonebook program from lab# 7. Make it possible for users to: 1) Store all entries in the phonebook into a location/file-name specified by the user. 2) Retrieve entries from the location/file-name specified by the user. If the user does not explicitly specify a path to the file, a default location of your choosing should be used.
Here is my current coplease add.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// create a struct
struct phonebook{
char fname[20];
char lname[20];
char phone[8];
};
void addfriend(struct phonebook **, int *);
void deletefriend(struct phonebook **, int);
void showPhonebook(struct phonebook **);
//four new methods added
void sortByFirstName(struct phonebook **);
void findNumber(struct phonebook **);
void randomSelect(struct phonebook **);
void deleteAll(struct phonebook **);
int main()
{
int i;
// create an array of type struct phonebook
struct phonebook **arr = (struct phonebook **)malloc(50 * sizeof(struct phonebook *));
// index at which new entry is to be added
int index = 0;
// set all entries of arr to NULL
for( i = 0 ; i < 50 ; i++ )
arr[i] = NULL;
while(1)
{
printf("Enter one of the following options ... ");
printf("1) Add friend 2) Delete friend 3) Show phone book 4) Sort By First Name 5) Find phobe number 6) Random Friend 7) Delete all 8) Quit ");
int option;
scanf("%d",&option);
switch(option)
{
case 1 : addfriend(arr, &index);
break;
case 2 : deletefriend(arr, index);
break;
case 3 : showPhonebook(arr);
break;
case 4 : sortByFirstName(arr);
break;
case 5 : findNumber(arr);
break;
case 6 : randomSelect(arr);
break;
case 7 : deleteAll(arr);
break;
case 8 : sortByFirstName(arr);
exit(0);
default : printf("Invlid choice , try again ! ");
exit(0);
}
}
return 0;
}
void addfriend(struct phonebook **arr, int *index)
{
struct phonebook *temp = (struct phonebook *)malloc(sizeof(struct phonebook));
printf("Enter the name of friend ");
scanf("%s%s", temp->fname, temp->lname);
printf("Enter the phone number ");
scanf("%s", temp->phone);
arr[*index] = temp;
(*index)++;
}
void deletefriend(struct phonebook **arr, int index)
{
char f[20], l[20];
printf("Enter the name of friend ");
scanf("%s%s", f, l);
int i;
// find the required contact
for( i = 0 ; i < 50 ; i++ )
// if contact is found
if(arr[i] && !strcmp(arr[i]->fname, f) && !strcmp(arr[i]->lname, l))
// set that element to NULL
arr[i] = NULL;
}
void showPhonebook(struct phonebook **arr)
{
int i;
for( i = 0 ; i < 50 ; i++ )
// if element is not NULL
if(arr[i])
printf("Name : %s %s Phone Number : %s ",arr[i]->fname, arr[i]->lname, arr[i]->phone);
}
//funtion to sort by first name
void sortByFirstName(struct phonebook **arr)
{
int i,j;
//three char pointer for storing the temp data
char *temp1= (char*)malloc(sizeof(char)*20);
char *temp2=(char*)malloc(sizeof(char)*20);
char *temp3=(char*)malloc(sizeof(char)*8);
for(i=0;i<50;i++)
{
if(arr[i])
for(j=i+1;j<50;j++)
{
if(arr[j])
if(strcmp(arr[j]->fname,arr[i]->fname)<0)//comparing the name
{
strcpy(temp1,arr[j]->fname);//copying to temp
strcpy(temp2,arr[j]->lname);
strcpy(temp3,arr[j]->phone);
strcpy(arr[j]->fname,arr[i]->fname);//changin array index value
strcpy(arr[j]->lname,arr[i]->lname);
strcpy(arr[j]->phone,arr[i]->phone);
strcpy(arr[i]->fname,temp1);
strcpy(arr[i]->lname,temp2);
strcpy(arr[i]->phone,temp3);
}
}
}
showPhonebook(arr);//printing tha names
}
void findNumber(struct phonebook **arr)
{
char f[20], l[20];
printf("Enter the name of friend ");
scanf("%s%s", f, l);
int i;
for( i = 0 ; i < 50 ; i++ ){
//if name found
if(arr[i] && !strcmp(arr[i]->fname, f) && !strcmp(arr[i]->lname, l)){
//printing the name
printf("Phone number : %s ",arr[i]->phone);
return;
}
}//if name not found
printf("Name not found ");
}
//method to select random friend
void randomSelect(struct phonebook **arr)
{
int i;
//in loop
while(1){
//generting rand no in between 0-50
i = rand() % 50;
if(arr[i])//if not null
{
printf("Name : %s %s Phone Number : %s ",arr[i]->fname, arr[i]->lname, arr[i]->phone);
break;
}
}
}
//method to dlete all names
void deleteAll(struct phonebook **arr){
int i;
for( i = 0 ; i < 50 ; i++ )
arr[i] = NULL;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
char file[4096];
//this condition means that the user has not passed the name of the file.
//use the default file name /File as the file to store the contacts of the phonebook.
if(argc==1)
{
strcpy(file,"/home/gaurav/File.txt");
}
else
{
strcpy(file,argv[1]);
}
FILE *fPtr;
int choice;
char fname[30],lname[30];
char phoneNumber[10];
char fullName[60];
char name[60],num[10];
fPtr=fopen(file,"a+");
while(1)
{
printf("1: Write entries to the phonebook. 2: Retrieve entries from the notebook 3: Quit Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
//Takes the pointer to end of the file
//fseek(fPtr,0,SEEK_END);
printf("Enter the first name : ");
scanf("%s",fname);
//strcat(fname," ");
printf("Enter the last name : ");
scanf("%s",lname);
strcat(lname," ");
printf("Enter the mobile number : ");
scanf("%s",phoneNumber);
strcat(phoneNumber," ");
fputs(fname,fPtr);
fputs(lname,fPtr);
fputs(phoneNumber,fPtr);
printf("The number is successfully added to the file. ");
break;
}
case 2:
{
printf("Enter the firstname : ");
scanf("%s",fname);
printf("Enter the last name : ");
scanf("%s",lname);
strcpy(fullName,fname);
strcat(fullName,lname);
strcat(fullName," ");
rewind(fPtr);
int f;
f=0;
while(fgets(name,60,fPtr)!=NULL)
{
if(strcmp(name,fullName)==0)
{
f=1;
break;
}
}
if(f==1)
{
fgets(num,10,fPtr);
printf("%s ",num);
}
else
{
printf("The entered entry is not found ");
}
break;
}
case 3:
{
break;
}
}
if(choice==3)
{
break;
}
}
fclose(fPtr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.