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

Assuming that the usephone2.cpp (listed below) is modified to add the following

ID: 3655734 • Letter: A

Question

Assuming that the usephone2.cpp (listed below) is modified to add the following statement:


cout << phonebook.search("Smith"); // displays Smith's phone number


Please modify Myphone class so that the above statement compiles and generates the desired output.


Please submit all files including that you modified. You must add comments to explain the changes made by you.


// usephone2.cpp

#include "myphone.h"

#include <fstream>

#include <iostream>

using namespace std;

#define MAX 50

int main()

{

MyPhone phonebook(MAX); // a phonebook of size 50

char name[31], num[21];

ifstream infile("phone.txt"); // open a file for reading

if (infile.fail())

{

cerr << "Failed to open data file ";

return 1;

}

// Read phone records from the file into char arrays.

while (! infile.eof())

{

infile >> name >> num;

// Ask phonebook to save the current phone record.

phonebook.addRecord(name, num);

} // while

infile.close(); // close the data file

// Ask phonebook to show phone records ordered by names.

cout << " Books by name:";

phonebook.byName();

cout << " ";

// Ask phonebook to show phone records ordered by phone numbers.

cout << " Books by number:";

phonebook.byPhone();

cout << " ";

cout << phonebook.search("Smith"); // displays Smith's phone number

//

// Tests when a name isn't not found.

//

cout << phonebook.search("Smih");

return 0;

}

// myphone.h declares MyPhone class

// A class serving as an interface to manipulate PhoneRec objects.

#include "phonerec.h"

#ifndef MYPHONE

#define MYPHONE

class MyPhone

{

PhoneRec *phonelist; // points to the first PhoneRec object.

int size; // The size of the PhoneRec array

int counter; // current counter

void sortName();

void sortPhone();

void showAll();

public:

MyPhone(int howmany); // constructor

~MyPhone(); // destructor

void addRecord(char *nm, char *ph); // adds a phone record to the phone list (the array)

void byName(); // shows records sorted by names

void byPhone(); // shows sorted by phone numbers

}; // MyPhone

#endif

// myphone.cpp implements MyPhone class

#include "myphone.h"

#include <string.h>

MyPhone::MyPhone(int n) // constructor

{ // n denotes the max. # of records

phonelist = new PhoneRec[n]; // allocate space dynamically

size = n;

counter = 0;

}

MyPhone::~MyPhone() // destructor

{

delete phonelist; // release the dynamically reserved space

}

void MyPhone::sortName()

{

PhoneRec temp;

// Sortthe records in ascending order by names.

for (int i=0; i < counter-1; ++i)

for (int k=i+1; k<counter; ++k)

if (strcmp(phonelist[i].getName(), phonelist[k].getName()) > 0)

{ /* swap */

temp = phonelist[i];

phonelist[i] = phonelist[k];

phonelist[k] = temp;

}

}

void MyPhone::sortPhone()

{

PhoneRec temp;

// Sortthe records in ascending order by phone numbers.

for (int i=0; i < counter-1; ++i)

for (int k=i+1; k < counter; ++k)

if (strcmp(phonelist[i].getPhone(), phonelist[k].getPhone()) > 0)

{ /* swap */

temp = phonelist[i];

phonelist[i] = phonelist[k];

phonelist[k] = temp;

}

}

void MyPhone::showAll()

{

for (int k=0; k < counter; ++k)

phonelist[k].showme();

}

void MyPhone::addRecord(char *nm, char *ph) // adds a phone record to the list

{

phonelist[counter].setName(nm); // save the name for the current record

phonelist[counter].setPhone(ph); // save the phone number for the currect record

++counter; // update the counter

}

void MyPhone::byName() // shows records sorted by names

{

sortName();

showAll();

}

void MyPhone::byPhone() // shows records sorted by numbers

{

sortPhone();

showAll();

}

// I know I have insert something here... this is what I got

void Myphone::phonebook.search(char *npho, )

{

for (int i=0; i =<n,i++)

if (strcmp(phonelist[i].npho(), getName()) > 0)

else found=true;

// phonerec.h

// The declaration file for the PhoneRec class.

#ifndef PHONEREC

#define PHONEREC

class PhoneRec

{

char name[31];

char phonenum[21];

public:

PhoneRec(); // default constructor

void setName(char *nm);

char * getName();

void setPhone(char *ph);

char * getPhone();

void showme();

}; // PhoneRec

#endif

// phonerec.cpp Implementation file for the PhoneRec class.

#include "phonerec.h"

#include <string>

#include <iostream>

using namespace std;

PhoneRec::PhoneRec() { }; // default constructor

void PhoneRec::setName(char *nm)

{ strcpy(name, nm); }

char * PhoneRec::getName()

{ return name; }

void PhoneRec::setPhone(char *ph)

{ strcpy(phonenum, ph); }

char * PhoneRec::getPhone()

{ return phonenum; }

void PhoneRec::showme()

{ cout << name << " " << phonenum << endl; }



I also attach the file if you can't read it


Explanation / Answer

// usephone2.cpp #include "myphone.h" #include #include using namespace std; #define MAX 50 int main() { MyPhone phonebook(MAX); // a phonebook of size 50 char name[31], num[21]; ifstream infile("phone.txt"); // open a file for reading if (infile.fail()) { cerr > name >> num; // Ask phonebook to save the current phone record. phonebook.addRecord(name, num); } // while infile.close(); // close the data file // Ask phonebook to show phone records ordered by names. phonebook.byName(); // Ask phonebook to show phone records ordered by phone numbers. phonebook.byPhone(); cout