Question

In: Computer Science

You have worked very hard on your phonebook program for many weeks and it is becoming...

You have worked very hard on your phonebook program for many weeks and it is becoming a popular application with all your friends. You decide you want to compete with facebook.com and the next upgrade of your phonebook software should make contact data accessible even if the user closes your application and returns to it at a later time. Add additional functionality to your previous phonebook program. 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. Answer needs to be in C Programming. Please and thank you.

Current code is:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

// Creates an array for the input from the user

struct phonebook{

char firstname[100];

char lastname[100];

char phonenumber[100];

};

void addfriend(struct phonebook **, int *);

void deletefriend(struct phonebook **, int);

void showPhonebook(struct phonebook **);

//Listed below are the 4 new functions for this code

void sortByFirstName(struct phonebook **);

void findNumber(struct phonebook **);

void randomSelect(struct phonebook **);

void deleteAll(struct phonebook **);

int main()

{

int i;

// This will create an array of type struct for phonebook

struct phonebook **arr = (struct phonebook **)malloc(50 * sizeof(struct phonebook *));

// This integer will be the index at which a new entry will be added

int index = 0;

// This part of the code will set all entries of arr to NULL

for( i = 0 ; i < 50 ; i++ )

arr[i] = NULL;

while(1)

{

//Prints the menu for the user to select an option

printf("Phone Book Application\n");
printf(" 1) Add friend\n");
printf(" 2) Delete friend\n");
printf(" 3) Show phone book\n");
printf(" 4) Sort by first name\n");
printf(" 5) Find a phone number\n");
printf(" 6) Randomly select a friend\n");
printf(" 7) Delete all\n");
printf(" 8) Exit\n");
printf("\nWhat would you like to do?: ");

int option;

scanf("%d",&option);

switch(option)

//This section of the code states cases based on the users input

{

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("Please enter a choice listed \n");

exit(0);

}

}

return 0;

}

void addfriend(struct phonebook **arr, int *index)

{

struct phonebook *temp = (struct phonebook *)malloc(sizeof(struct phonebook));

printf("Please enter the name of the friend\n");

scanf("%s%s", temp->firstname, temp->lastname);

printf("Please enter their phone number\n");

scanf("%s", temp->phonenumber);

arr[*index] = temp;

(*index)++;

}

void deletefriend(struct phonebook **arr, int index)

{

char f[100], l[100];

printf("Please enter the name of the friend\n");

scanf("%s%s", f, l);

int i;

// This part of the code will locate the required contact

for( i = 0 ; i < 50 ; i++ )

// This part of the code will display the contact if found

if(arr[i] && !strcmp(arr[i]->firstname, f) && !strcmp(arr[i]->lastname, l))

// This part of the code sets arr to NULL

arr[i] = NULL;

}

void showPhonebook(struct phonebook **arr)

{

int i;

for( i = 0 ; i < 50 ; i++ )

// This part of the code will display the contact if arr is is not NULL

if(arr[i])

printf("Name : %s %s\nPhone Number : %s\n\n",arr[i]->firstname, arr[i]->lastname, arr[i]->phonenumber);

}

// This part of the code creates a function that will sort the phonebook entries by first name

void sortByFirstName(struct phonebook **arr)

{

int i,j;

// This part of the code helps store the temp data

char *temp1= (char*)malloc(sizeof(char)*100);

char *temp2=(char*)malloc(sizeof(char)*100);

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]->firstname,arr[i]->firstname)<0)// This part of the code will compare the names found in the phonebook

{

strcpy(temp1,arr[j]->firstname);// This part of the code will copy to the temp

strcpy(temp2,arr[j]->lastname);

strcpy(temp3,arr[j]->phonenumber);

strcpy(arr[j]->firstname,arr[i]->firstname);// This part of the code will change te array index value

strcpy(arr[j]->lastname,arr[i]->lastname);

strcpy(arr[j]->phonenumber,arr[i]->phonenumber);

strcpy(arr[i]->firstname,temp1);

strcpy(arr[i]->lastname,temp2);

strcpy(arr[i]->phonenumber,temp3);

}

}

}

showPhonebook(arr);// This part of the code will print all of the names found in the phonebook

}

void findNumber(struct phonebook **arr)

{

char f[20], l[20];

printf("Please enter the name of the friend\n");

scanf("%s%s", f, l);

int i;

for( i = 0 ; i < 50 ; i++ ){

// This part of the code will attempt to locate the friend's name in the phonebook

if(arr[i] && !strcmp(arr[i]->firstname, f) && !strcmp(arr[i]->lastname, l)){

// This part of the code will print the phone number if the name is located.

printf("Phone number : %s\n",arr[i]->phonenumber);

return;

}

}//if name not found

printf("Sorry, but this name was not found. \n");

}

// This part of the code allows the user to select a random friend from the phonebook

void randomSelect(struct phonebook **arr)

{

int i;

//This part of the code creates a loop

while(1){

// This part of the code will generate a random number between 0 and 50

i = rand() % 50;

if(arr[i])// If the number generated is not between the two prior numbers, it will be NULL

{

printf("Name : %s %s\nPhone Number : %s\n\n",arr[i]->firstname, arr[i]->lastname, arr[i]->phonenumber);

break;

}

}

}

// This part of the code will allow the user to delete all of the entries in the phonebook

void deleteAll(struct phonebook **arr){

int i;

for( i = 0 ; i < 50 ; i++ )

arr[i] = NULL;

}

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

//Function trial product

void ADD();

void Delete1();

void Display1();

void Alphabetical1();

void Number1();

void Random1();

void DeleteAll1();

typedef struct Phone_Book_List {

char *FName;

char *LName;

char *PhoneNumber1;   

} list;

//delete entry

typedef struct Delete_Entry {

char *FName;

char *LName;   

} take;

//Pointers in to the structures

list *lt1;

take *tk1;

//variables are declared in globally

int cnt = 0;

int delCnt = 0;

int main(void)

{

//VARIABLE in declarations

int iSel;

do {

printf("\n\nPHONE BOOK:\n\n");

printf("1) ADD FRIEND\n");

printf("2) DELETE FRIEND\n");

printf("3) DISPLAY PHONE BOOK\n");

printf("4) SORT AND DISPLAY ENTRIES ALPHABETICALLY BY LAST NAME\n");

printf("5) FIND PHONE NUMBER FOR GIVEN NAME\n");

printf("6) RANDOMLY SELECT CONTACT\n");

printf("7) DELETE ALL CONTACTS\n");

printf("8) EXIT\n");

printf("WHAT DO YOU WANT TO DO? ");   

scanf("%d", &iSel);   

switch (iSel) {   

case 1: //Add entrance toward phonebook

ADD();

break;

case 2: //Delete access toward phonebook

Delete1();

break;   

case 3: //Display every access toward phonebook

Display1();

break;   

case 4: //Sort entries alphabetically in phonebook

Alphabetical1();

break;

case 5: //Find phone number toward phonebook

Number1();

break;

case 6: //Randomly choose contact toward phonebook

Random1();

break;

case 7: //Delete every one contacts toward phonebook

DeleteAll1();

break;   

case 8: //CLOSE THE PROGRAM

break;   

default: //while user has been entered wrong access

printf("\nYou entered an invalid selection. Try again.\n");

break;   

}   

} while (iSel != 8); //END DO WHILE LOOP

//RETURN USED MEMORY

free(tk1);

free(lt1);

tk1 = NULL;

lt1 = NULL;   

return 0;   

} //END MAIN

//FUNCTION DEFINITIONS

//ADD AN ENTRY

void ADD()

{

char *leftName;

if (cnt == 0)

{

lt1 = (list *) malloc ((cnt*25) + 25);

}

else

{

lt1 = (list *) realloc (lt1, (cnt*50) + 50);

}   

if (lt1 == NULL)

{

printf("YOU CANNOT ADD MORE MEMORY\n");

}

else

{

lt1[cnt].FName = (char *) malloc(sizeof(char)*15);

lt1[cnt].LName = (char *) malloc(sizeof(char)*15);

lt1[cnt].PhoneNumber1 = (char *) malloc(sizeof(char)*15);

printf("\nENTER THEIR FIRST NAME: ");

scanf("%s", lt1[cnt].FName);

printf("\nENTER THEIR LAST NAME: ");

scanf("%s", lt1[cnt].LName);

printf("\nENTER THEIR PHONE NUMBER: ");

scanf("%s", lt1[cnt].PhoneNumber1);

printf("\nCONTACT ADDED\n");   

}

cnt++;

}//END FUNCTION

//DELETE AN ENTRY

void Delete1()

{

int i;

int q = 0;

char *uName;

if (delCnt == 0)

{

tk1 = (take *) malloc ((delCnt*25) + 25);

}

else

{

tk1 = (take *) realloc (tk1, (delCnt*1) + 1);

}

if (tk1 == NULL)

{

printf("This cannot be deleted (out of memory)\n");

}

else

{

tk1[delCnt].FName = (char *) malloc(sizeof(char)*15);

tk1[delCnt].LName = (char *) malloc(sizeof(char)*15);

printf("\nENTER THEIR FIRST NAME: ");

scanf("%s", tk1[delCnt].FName);

printf("\nENTER THEIR LAST NAME: ");

scanf("%s", tk1[delCnt].LName);

}

for (i = 0; i < cnt; i++)

{

if (lt1[i].FName == NULL && lt1[i].LName == NULL) continue;

if (strcmp(lt1[i].FName, tk1[delCnt].FName) == 0 && strcmp(lt1[i].LName, tk1[delCnt].LName) == 0)

{

printf("\n%s %s has been deleted\n", lt1[i].FName, lt1[i].LName);

lt1[i].FName = NULL;

lt1[i].LName = NULL;

lt1[i].PhoneNumber1 = NULL;

q = 1;

break;

}

} //End for loop   

if (q != 1)

{

printf("\nThis person is not in the Phonebook\n");

)

delCnt++;

cnt--;

}

void Display1()

{

int i;

printf("\nYOUR CONTACTS:\n");

for (i = 0; i < cnt; i++)

{

if (lt1[i].FName != NULL && lt1[i].LName != NULL)

{

printf("\n%s %s: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);

}

}//End for loop

system("pause");

}

//Sort entries alphabetically by last name.

void Alphabetical1()

{

int i;

int j;

char temp[50][50];

printf("\nYOUR CONTACTS:\n");  

for (i = 0; i < cnt; i++)

{

for (j = i + 1; j < cnt; j++)

{

if (strcmp(lt1[i].LName, lt1[j].LName) > 0)

{

strcpy(temp[i], lt1[i].LName);

strcpy(lt1[i].LName, lt1[j].LName);

strcpy(lt1[j].LName, temp[i]);

}

}

}//End for loop

for (i = 0; i < cnt; i++)

printf("\n%s %s: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);

system("pause");

}//End function

void Number1()

{

int i;

int q = 0;

char fName[25];

char lName[25];   

printf("\nEnter their First Name: ");

scanf("%s", fName);

printf("\nEnter their Last Name: ");

scanf("%s", lName);   

for (i = 0; i < cnt; i++)

{

if (strcmp(lt1[i].FName, fName) == 0 && strcmp(lt1[i].LName, lName) == 0)

{

printf("\n%s %s's PHONE NUMBER IS: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);

q = 1;

break;

}

}

if (q != 1)

{

printf("\nThis person is not in the Phonebook\n");

}

system("pause");

}

void Random1()

{

srand(time(NULL));

int iRandomNum;  

iRandomNum = (rand() % cnt) + 1;

printf("%s %s: %s\n",

lt1[iRandomNum].FName, lt1[iRandomNum].LName, lt1[iRandomNum].PhoneNumber1);

system("pause");

}

void DeleteAll1()

{

int i;

for (i = 0; i < cnt; i++)

{

do{   

lt1[i].FName = NULL;

lt1[i].LName = NULL;

lt1[i].PhoneNumber1 = NULL;

break;

} while (i <= cnt);

}

printf("\nALL CONTACTS DELETED\n\n");

system("pause");

}//End function


Related Solutions

You worked very hard during the summer, and now you are paid $5,000 as your compensation....
You worked very hard during the summer, and now you are paid $5,000 as your compensation. Now you would like to invest all of them into a fund so that you can spend them after 5 years. You have 2 funds to choose from: 1. Fund A promises 8% annual interest payment without reinvestment 2. Fund B promises 7% annual interest payment with reinvestment of interest Which one would you choose? Please state clearly your criteria and the formulas you...
Most video games have several difficulty settings, from "Easy" to "Hard" (many of these have very...
Most video games have several difficulty settings, from "Easy" to "Hard" (many of these have very colorful names). A video game designer wants to determine how to structure the difficulty levels for a new game so that the average time it takes the typical player to play through "Hard" mode is longer than the time to play through "Easy" mode. A sample (Group 1) of 15 typical players took an average of 5.3 hours to complete the game on "Hard",...
You have worked hard over the last three years to save up enough money for a...
You have worked hard over the last three years to save up enough money for a down payment on your first home. After meeting with your lender, you are faced with two loan options. Both loans are 30 year, fixed rate mortgages with payments of $1,800 per month. Origination fees will be 2% of the loan amount for either loan. Loan A has a contract interest rate of 3.0% with no points, while Loan B has a rate of 2.75%...
You have worked hard over the last three years to save up enough money for a...
You have worked hard over the last three years to save up enough money for a down payment on your first home. After meeting with your lender, you are faced with two loan options. Both loans are 30 year, fixed rate mortgages with payments of $1,800 per month. Origination fees will be 2% of the loan amount for either loan. Loan A has an APR of 3.0% with no points, while Loan B has an APR of 2.75% with 1.25...
Think about your marginal product of labor. If you worked or have worked at a job,...
Think about your marginal product of labor. If you worked or have worked at a job, what were the factors that made you more productive? List 4. Did your co-workers make you more or less productive? list two reasons. If you have not worked, use being a student as your example.
Your new friend Angie is very excited to be 2 weeks pregnant and have a baby.  However,...
Your new friend Angie is very excited to be 2 weeks pregnant and have a baby.  However, she’s also very nervous.  She tells you that her sister had two babies with spinal muscular atrophy type I (SMA1) (recessively inherited terminal condition, typically leading death by age 2).  She’s thinking about genetic testing, but doesn’t know who to test (herself, her boyfriend (the future-baby’s genetic father), the fetus, her sister, etc.), or what type of test to get (carrier testing, non-invasive prenatal testing, etc.).  What...
It is time to buy a new car. You worked hard and want to buy an...
It is time to buy a new car. You worked hard and want to buy an expensive car (Porsche Cayenne) for $100,000. Porsche dealership will gladly finance your new purchase. However, the only way for you to be financed is to show them that you have 20% of the money needed. As soon as you have that much money in your account, the dealership will finance your new vehicle. You start saving for 52 months with the APR of 7.5%,...
Brazil is a very large emerging market that is becoming increasingly important in many MNEs’ global...
Brazil is a very large emerging market that is becoming increasingly important in many MNEs’ global supply chains. Discuss what criteria should the companies use to make a decision on where to manufacture the good and whether to outsource the manufacturing or control it internally? What are the advantages and disadvantages of sourcing from Brazil as opposed to other countries? Why?
Telehealth is becoming very popular in many areas, giving patients access to quality care when they...
Telehealth is becoming very popular in many areas, giving patients access to quality care when they need it. Previously, telehealth was reserved for remote areas, but we are seeing a shift in care so that these health services are available to many people that need them. However, this system presents several challenges. Suppose that a patient, provider and telehealth proxy are all in different states. If there is a malpractice claim, which state should it be filed in? What about...
Older people often have a hard time finding work. AARP reported on the number of weeks...
Older people often have a hard time finding work. AARP reported on the number of weeks it takes a worker aged 55 plus to find a job. The data on number of weeks spent searching for a job contained in the table below. 17 0 0 28 18 37 7 13 33 0 24 50 4 48 5 19 3 25 2 29 4 8 8 32 1 27 7 47 6 0 a. Provide a point estimate of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT