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

In c/c++ programming Add additional functionality to your phonebook program from

ID: 3600894 • 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 #include #include // 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

Here is the code in c++

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

// 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 **);

void saveToFile(char fileName[50], struct phonebook **arr);
void readFromFile(char fileName[50], struct phonebook **arr, int *index);
unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch);

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;

   char fileName[50];
   // 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 phone number 6) Random Friend 7) Delete all 8) Save to file 9) Read from file 10) 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:
           printf("Enter name of file ");
           scanf("%s", fileName);
           saveToFile(fileName, arr);
           break;
       case 9:
           printf("Enter name of file ");
           scanf("%s", fileName);
           readFromFile(fileName, arr, &index);
           break;
       case 10:
           exit(0);
           break;
       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;
}

void saveToFile(char fileName[50], struct phonebook **arr)
{
   std::ofstream outStream(fileName);
   int i;
   char temp[500];
   for (i = 0; i < 50; i++)
       // if element is not NULL
       if (arr[i])
       {
           sprintf(temp, "Name : %s %s Phone Number : %s ", arr[i]->fname, arr[i]->lname, arr[i]->phone);
           outStream << temp;
       }
}
void readFromFile(char fileName[50], struct phonebook **arr, int *index)
{
   std::ifstream inStream(fileName);
   string line;
   vector<string> words;
   int totalWords = 0;

   if (inStream.is_open())
   {
       while (getline(inStream, line))
       {
           if (strcmp(line.c_str(), "") == 0)
           {
               continue;
           }
           else
           {
               totalWords = split(line, words, ' ');
               if (totalWords > 0 && strcmp(words[0].c_str(), "Name ") == 0)
               {
                   arr[*index] = (struct phonebook *)malloc(sizeof(struct phonebook));
                   if (totalWords == 3)
                   {
                       strcpy(arr[*index]->fname, words[2].c_str());
                       strcpy(arr[*index]->lname, "");
                   }

                   else if (totalWords == 4)
                   {
                       strcpy(arr[*index]->fname, words[2].c_str());
                       strcpy(arr[*index]->lname, words[3].c_str());
                   }
                   else
                   {
                       strcpy(arr[*index]->fname, "");
                       strcpy(arr[*index]->lname, "");
                   }

                   if (getline(inStream, line))
                   {
                       totalWords = split(line, words, ' ');

                       if (totalWords > 0 && strcmp(words[0].c_str(), "Phone ") == 0)
                       {
                           if (totalWords == 4)
                           {
                               strcpy(arr[*index]->phone, words[3].c_str());
                           }
                           else
                           {
                               strcpy(arr[*index]->phone, "");
                           }
                       }
                   }
                   else
                   {
                       strcpy(arr[*index]->phone, "");
                   }
                   (*index)++;
                  
               }
           }
       }
       inStream.close();
   }

   else
       cout << "Unable to open file";


}

unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
   unsigned int pos = txt.find(ch);
   unsigned int initialPos = 0;
   strs.clear();

   // Decompose statement
   while (pos != std::string::npos) {
       strs.push_back(txt.substr(initialPos, pos - initialPos + 1));
       initialPos = pos + 1;

       pos = txt.find(ch, initialPos);
   }

   // Add the last one
   strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));

   return strs.size();
}

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