Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c/c++ Add additional functionality to your phonebook program from lab# 7. Mak

ID: 3599971 • Letter: I

Question

In c/c++ 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 <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 50

struct Phone_record
// declare all variables
{
char fname[MAX];
char lname[MAX];
char category[MAX];
char phonenumber[MAX];
int count;
};

struct Phone_list
{
int count;   // keeps rack of number of ppl in list
struct Phone_record pr[MAX];
};

void displaymenu(void);
void choices(void);
void add_record(struct Phone_record *);
void del_record(struct Phone_list *);
void save_record(struct Phone_record *);
void load_record(struct Phone_record *);
void print_record(const struct Phone_record *);

void displaymenu()
{

printf(   "A-Add a Phone Number "
"D-Delete a Phone Number "
"S-Save the phone book "
"L-Load the phone book "
"P-Printf the Phone List "
"Q-Quit the program "
"H-Help Menu ");  
}  

void choices(void)
{

struct Phone_record pst;
char pick='x';
pst.count=0;
//   scanf("%c",&pick);
printf(" Please make your selection:   ");
while (pick!='Q')
{
scanf("%c",&pick);
switch (pick)
{
case 'A':
add_record(&pst);
break;
case 'D':
//del_record();
break;
case'S':
save_record(&pst);
break;
case'L':
load_record(&pst);
break;
case 'P':
print_record(&pst);
break;
case 'H':
displaymenu();
break;
case 'Q':
break;
default:
printf("your choice was invalid ");
break;
}
}
}

void add_record(struct Phone_record *pst)
{
printf("pleaes enter the last name ");
scanf("%s",pst->lname);
printf("please enter the first name ");
scanf("%s",pst->fname);
printf("plese enter a category ");
scanf("%s",pst->category);
printf("plese enter your phone number ");
scanf("%s",pst->phonenumber);
}

void del_record(struct Phone_list *book)
{

printf("deleting the record");

}

void save_record(struct Phone_record *pst)
{
//   pfile=fopen("c:\myfile.txt","w");
/*fprintf(file,"%d ",(*book.count));
for i=0;   i<(*book).count;   i++);
{
fprintf("file,%s ",(*book.Phone_list[i].name);
fprintf("file,%s ",(*book.Phone_list[i].category);
fprintf("file,%s ",(*book.Phone_list[i].phonenumber);

}
*/
}
void load_record(struct Phone_record *pst)
{
pfile=fopen("c:\myfile.txt","r");
fscanf(pfile,"%d",&(pst.count);
for (i=0;   i<pst.count;   i++;)
{
fscanf(pfile,"%s %s",pst[i]->fname, pst[i]->lname);
fscanf(pfile,"%s",pst[i]->category);
fscanf(pfile,"%s",pst[i]->phonenumber);
}
}
void print_record(const struct Phone_record *pst)
{
printf(" your name is %s %s ",pst->fname, pst->lname);
printf("your category is %s ",pst->category);
printf(" your phone number is %s ",pst->phonenumber);
choices();
}

int main (int argc, char*argv[1])
{
FILE *pfile=NULL;   // File Pointer
struct Phone_record;
int i;
pfile=fopen("C:\myfile.txt","w");
printf("Welcome to the Phone Book");
printf(" ");
displaymenu();
choices();
fclose(pfile);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote