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

I need to make an addition to a code that i alreay compiled i need to add file I

ID: 3801947 • Letter: I

Question

I need to make an addition to a code that i alreay compiled i need to add file I/O and Fucntions to the code for iths

Complete phonebook application program by adding saving and loading of the phone book on disk. Also each feature of the application should be in its own function.

the application code is already made, please help me find a way to

Complete phonebook application program by adding saving and loading of the phone book on disk. Also each feature of the application should be in its own function.

Thank You!

-----------------------------------------------

We would like to write a phone book application with the following features.

1- Add a Phone Record: This asks user for the name and the phone number and adds it into the phone book.

2- Search By Name: This asks user to enter a name. Given the name, the program displays the phone number of the record with that name.

3- Search By Phone: This asks user to enter a phone number. Given the phone number, the program displays the name of the record with that phone number.

4- Display All: Reports all the records with their names and phone numbers on screen.

------------------------------------------------------

this was the code

----------------------

# include <iostream>
# include < string >
using namespace std;

int main()
{

   // int number [10] number can be 10, 200 ...
   // int number [3] number can be (1,5,3) -- sequnce of values

   // int num1; // num1 = 5 --- num1 = int
   // int num2; [3]; // nu,2 = 1,5,3 ; indeces 0.1.2.3 ... n ---- num2 int,int,int   -- 3 int for the intnum2

   // int num2; [3];
   // num2[0] = 5;

   //int num2[3] = 5;
   //num2[0] = 10;
   //num2[2] = 15;

   //for (int i = 0; i < 3; i++)
   //{
   //   cout << num2[i] << endl;
   //}


   // do not want to do any math on these numbers ,, want to keep them just as is -- thats why include as string
   string name[100]; // name[0], name[1], name[2], name[3], .... name[99], --- last intergere will be number of records - 1 ... so name[99]
   string number[100]; // name[0], name[1], name[2], name[3], .... name[99], --- last intergere will be number of records - 1 ... so name[99]
   int count = 0;
   int choice;
   for (int i = 0; i < 100; i++)
   {
       name[i] = "";
       number[i] = "";
   }
   //name[0]<-> number[0] // parallel arrays // run parallel to each other and the values correspond
   // each item inside name corresponds to each item inisde umber


   //We would like to write a phone book application with the following features.
   //1 - Add a Phone Record : This asks user for the name and the phone number and adds it into the phone book.
   //2 - Search By Name : This asks user to enter a name.Given the name, the program displays the phone number of the record with that name.
   //3 - Search By Phone : This asks user to enter a phone number.Given the phone number, the program displays the name of the record with that phone number.
   //4 - Display All : Reports all the records with their names and phone numbers on screen.

   // step 2: Create a program menu
   // step 3: Enclose menu inside loop to repeat
   // step 4: Create the if statemetsn inside loop for each feature
   // step 5: Implement each feature


   do
   {

       cout << "***Welcome to Phone Book Application!***" << endl;
       cout << "1 - Add a Phone Record" << endl;
       cout << "2 - Search by Name" << endl;
       cout << "3 - Search by Phone" << endl;
       cout << "4 - Display All: Report: " << endl;
       cout << "0 - Exit" << endl;
       cout << "What do you wan to do?" << endl;
       cin >> choice;

       if (choice == 1) //Feature #1
       {
           string nameToAdd;
           cout << "What is name? ";
           cin >> nameToAdd;

           string numberToAdd;
           cout << "What is Phone number? ";
           cin >> numberToAdd;

           // add the name and the number to program data
           name[count] = nameToAdd;
           number[count] = numberToAdd;

           count++;
       }
       else if (choice == 2) // Feature # 2
       {
           cout << "what is name? " << endl;
           string nameSearch;
           cin >> nameSearch;
           bool found = false;

           for (int i = 0; i < count; i++)
           {
               if (name[i] == nameSearch)
               {
                   cout << "Number is " << number[i] << endl;
                   found = true;
               }
           }
           if (found == false)
           {
               cout << "Name Unidentified! " << endl;
           }
       }

       else if (choice == 3) // Feature # 3
       {

           cout << "what is Number? " << endl;
           string numberSearch;
           cin >> numberSearch;
           bool found = false;

           for (int i = 0; i < count; i++)
           {
               if (number[i] == numberSearch)
               {
                   cout << "name is " << name[i] << endl;
                   found = true;
               }
           }

           if (found == false)
           {
               cout << "Number Unidentified! " << endl;
           }
       }

       else if (choice == 4) // Feature # 4
       {

           for (int i = 0; i < count; i++)
           {
               cout << "Name: " << name[i] << endl;
               cout << "Number: " << number[i] << endl;
           }
       }

       cout << "" << endl;

   } while (choice != 0);

   system("pause");
   return 0;
}


------------------------------------------------------

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct Phonebook_Contacts

   {

      char FirstName[30];

      char LastName[30];

      char PhoneNumber[30];

   } phone;

void AddEntry(phone * );

void DeleteEntry(phone * );

void PrintEntry(phone * );

void SortByLastName(phone*);

void SortByFirstName(phone*);

void SearchForNumber(phone * );

void RandomName(phone * );

void DeleteAll(phone * );

void FreeContacts (phone * );

void SaveFile(phone * );

void RetrieveFile(phone * );

int count = 0;

int counter = 0;

char FileName[256];

FILE *pRead;

FILE *pWrite;

list *lt;

int main (void)

   {     

      phone *phonebook;

      phonebook = (phone*) malloc(sizeof(phone)*100);

      int iSelection = 0;

         if (phonebook == NULL)

         {

         printf("Out of Memory. The program will now exit");

         return 1;

         }

         else {}

      do

      {

         printf(" Phonebook Options");

         printf(" (1) Add Contact");

         printf(" (2) Delete Contact");

         printf(" (3) Display Phonebook ");

         printf(" (4) Sort Contacts by first name");

         printf(" (5) Sort Contacts by last name");

         printf(" (6) Look Up a Contacts Phone Number");

         printf(" (7) Pick a Random Contact to Call");

         printf(" (8) Delete ALL Contacts");

         printf(" (9) Store all contacts in a file");

         printf(" (10) Retrieve contacts from a file");

         printf(" (11) Exit Phonebook");

         printf(" Select an option from the menu? ");

         scanf("%d", &iSelection);

         if (iSelection == 1)

         {

            AddEntry(phonebook);

         }

         if (iSelection == 2)

         {

            DeleteEntry(phonebook);

         }

         if (iSelection == 3)

         {

            PrintEntry(phonebook);

         }

         if (iSelection ==4)

         {

            SortByFirstName(phonebook);

          }

         if (iSelection ==5)

         {

            SortByLastName(phonebook);

                        }

         if (iSelection == 6)

         {

            SearchForNumber(phonebook);        

         }

         if (iSelection == 7)

         {

            RandomName(phonebook);        

         }

         if (iSelection == 9)

         {

            DeleteAll(phonebook);

         }

         if (iSelection == 10)

         {

            StoreFile(phonebook);        

         }

         if (iSelection == 8)

         {

            RetrieveFile(phonebook);

         }

         if (iSelection == 11)

         {

            printf(" You have chosen to exit the Phonebook. ");

            system("pause");

            FreeContacts(phonebook);

            return 0;

         }              

      } while (iSelection <= 13);

   }

void AddEntry (phone * phonebook)

{

   pWrite = fopen("phonebook_contacts.dat", "a");

   if ( pWrite == NULL )

   {

      perror("The following error occurred ");

      exit(EXIT_FAILURE);

   }

      else

      {

         counter++;

         realloc(phonebook, sizeof(phone));

         printf(" First Name: ");

         scanf("%s", phonebook[counter-1].FirstName);

         printf("Last Name: ");

         scanf("%s", phonebook[counter-1].LastName);

         printf("Phone Number XXX-XXX-XXXX: ");

         scanf("%s", phonebook[counter-1].PhoneNumber);

         printf(" Contact successfully added to Phonebook ");

         fprintf(pWrite, "%s %s %s ", phonebook[counter-1].FirstName, phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);

         fclose(pWrite);

      }

}

void DeleteEntry (phone * phonebook)

{

   int x = 0;

   int i = 0;

   char deleteFirstName[20]; //

   char deleteLastName[20];

      printf(" First name: ");

      scanf("%s", deleteFirstName);

      printf("Last name: ");

      scanf("%s", deleteLastName);

      for (x = 0; x < counter; x++)

      {

         if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)

         {

            if (strcmp(deleteLastName, phonebook[x].LastName) == 0)

            {

                for ( i = x; i < counter - 1; i++ )

               {

                  strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);

                  strcpy(phonebook[i].LastName, phonebook[i+1].LastName);

                  strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);

               }

               printf("Record deleted from the phonebook. ");

               --counter;

               return;

            }

         }

      }   

   printf("That contact was not found, please try again.");

}

void PrintEntry (phone * phonebook)

{

   int x = 0;

   printf(" Phonebook Entries: ");

   pRead = fopen("phonebook_contacts.dat", "r");

   if ( pRead == NULL)

   {

      perror("The following error occurred: ");

      exit(EXIT_FAILURE);

   }

   else

   {

      for( x = 0; x < counter; x++)

      {

         printf(" (%d) ", x+1);

         printf("Name: %s %s ", phonebook[x].FirstName, phonebook[x].LastName);

         printf("Number: %s ", phonebook[x].PhoneNumber);

      }

   }

   fclose(pRead);

}

  void SortByFirstName (phone * phonebook)

{

   int i = 0;

   int x = 0;

   int swap;

   int TempCounter = counter;

   phone Temp;

   do

   {

      swap = 0;

      for(i = 1; i < TempCounter; i++)

      {

         if(strcmp(phonebook[i-1].FirstName, phonebook[i].FirstName) > 0)

         {

            Temp = phonebook[i];

            phonebook[i] = phonebook[i-1];

            phonebook[i-1] = Temp;

            strcpy(Temp.FirstName, phonebook[i].FirstName);

            strcpy(Temp.LastName, phonebook[i].LastName);

            strcpy(Temp.PhoneNumber, phonebook[i].PhoneNumber);

            swap = 1;

         }

      }

      TempCounter--;

   } while (swap);

   printf(" Your Contacts listed in Alphabetical Order by First Name: ");

   for( x = 0; x < counter; x++ )

   {

      printf(" (%d) ", x+1);

      printf("Name: %s %s ", phonebook[x].FirstName, phonebook[x].LastName);

      printf("Number: %s ", phonebook[x].PhoneNumber);

   }  

}

void SortByLastName (phone * phonebook)

   {

   int i = 0;

   int x = 0;

   int swap;

   int TempCounter = counter;

   phone Temp;

   do

   {

      swap = 0;

      for(i = 1; i < TempCounter; i++)

      {

         if(strcmp(phonebook[i-1].LastName, phonebook[i].LastName) > 0)

         {

            Temp = phonebook[i];

            phonebook[i] = phonebook[i-1];

            phonebook[i-1] = Temp;

            strcpy(Temp.FirstName, phonebook[i].FirstName);

            strcpy(Temp.LastName, phonebook[i].LastName);

            strcpy(Temp.PhoneNumber, phonebook[i].PhoneNumber);

            swap = 1;

         }

      }

      TempCounter--;

   } while (swap);

   printf(" Your contacts listed in Alphabetical Order by Last Name: ");

   for( x = 0; x < counter; x++ )

   {

      printf(" (%d) ", x+1);

      printf("Name: %s %s ", phonebook[x].FirstName, phonebook[x].LastName);

      printf("Number: %s ", phonebook[x].PhoneNumber);

   }                

}

void SearchForNumber (phone * phonebook)

{

   int x = 0;

   char TempFirstName[20];

   char TempLastName[20];

   printf(" What is the Name of the Contact that you want to find the Phone Number for?");

   printf(" First Name: ");

   scanf("%s", TempFirstName);

   printf("Last Name: ");

   scanf("%s", TempLastName);

   for (x = 0; x < counter; x++)

   {

      if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)

      {

         if (strcmp(TempLastName, phonebook[x].LastName) == 0)

         {

            printf(" %s %s's phone number is %s ", phonebook[x].FirstName, phonebook[x].LastName, phonebook[x].PhoneNumber);

         }

      }

   }    

}

void RandomName (phone * phonebook)

{

   int iRandom = 0;

   srand(time(NULL));

   iRandom = rand() % counter;

   int x = iRandom;

   printf(" Your random Contact is: %s %s ", phonebook[x].FirstName, phonebook[x].LastName);

   printf("Their phone number is: %s ", phonebook[x].PhoneNumber);

}   

void DeleteAll (phone * phonebook)

{

   int x = 0;

   char nullStr[20] = {''};

   for ( x = 0; x < counter; x++ )

   {

      strcpy(phonebook[x].FirstName, nullStr);

      strcpy(phonebook[x].LastName, nullStr);

      strcpy(phonebook[x].PhoneNumber, nullStr);

      --counter;

   }  

   printf("All Contacts have been deleted. ");     

}

void FreeContacts (phone * phonebook)

{

     --counter;

     for ( ; counter > 0; --counter)

     {

        free(phonebook[counter].FirstName);

        free(phonebook[counter].LastName);

        free(phonebook[counter].PhoneNumber);

        free(phonebook);

        counter = 0;

        return;

     }

     void StoreFile()

{

     int iPick;

     char FileName[50];

     int i;

       

       

     printf(" 1) Name the file yourself or 2) Use the default name(phone_book.txt) Select your option (enter 1 or 2): ");

     scanf("%d", &iPick);

                              

     if (iPick == 2)

     {

     pWrite = fopen("phone_book.txt", "w");    

     }

     else

     {

         printf("File Name:");

         scanf("%s", FileName);

         pWrite = fopen("FileName", "w");

     }

                              

     if (pWrite == NULL)

     {

     printf(" File cannot be opened ");

     }

     else

     {

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

         {

                fprintf(pWrite, "%s %s %s ", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);

                fclose(pWrite);

         }

           

         printf(" Contacts saved to file ");

     }  

        

}

  

void RetrieveFile()

{

     char rFileName[50];

     int i;

       

     printf(" Enter File Name: ");

     scanf("%s", rFileName);

  

     pRead = fopen("rFileName", r);

  

     if (pRead == NULL)

     {

     printf(" File cannot be opened ");

     }

     else

     {

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

         {

                fscanf(pWrite, "%s %s %s ", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);

                printf(" %s %s %s", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);

                  

         }

}