Hello!
I am new to this forum and I am also relatively new to C++. I am currently taking a C++ programming class, and I could really use some help with a program.
I already completed the first part of the assignment, in which I
implemented a software-based directory to replace AT&T paper phonebooks. I will include my code for this below.
PROBLEM: AT&T has decided paper phone books are stupid and wasteful. They wanted you to implement a software-based directory instead. So you did this. (This was the previous assignment, which I completed). Unfortunately, the job of
a software developer is never complete, and AT&T had some modifications for you to make.
You coded a system which uses a static array of pointers to reference the phonebook’s DirectoryEntry objects. Unfortunately, that had a fixed size of 100, and AT&T wasn’t happy with that. They want you to instead implement a new DirectoryEntriesList class which functions as a linked list to encapsulate and organize your DirectoryEntry objects. This will replace the old DirectoryEntry array.
New .h files have been posted. There is a directoryentrieslist.h file, which
contains the interface to the DirectoryEntriesList class. It also contains very
slightly modified phonebook.h and directoryentry.h files.
Here is the new work you must complete for full credit in this assignment:
1) Implement the DirectoryEntriesList class methods given in
directoryentrieslist.h.
2) Modify your old .cpp (phonebook.cpp and directoryentry.cpp) files to properly
work with the new linked list DirectoryEntriesList object.
__________________________________________
Here is the directoryentrieslist.h header file (they provided):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#ifndef DIRECTORY_ENTRY_LIST_H
#define DIRECTORY_ENTRY_LIST_H
#include <string>
#include "directoryentry.h"
using namespace std;
class DirectoryEntriesList{
public:
//Constructor; initially empty
DirectoryEntriesList();
//Destructor
~DirectoryEntriesList();
//Returns the size of the linked list
int size();
//Print the linked list in ascending order by Name to std out
void print();
//Insert a new directory entry. The DirectoryEntriesList should always remain in ascending order by Name.
void insert(DirectoryEntry& t);
//Returns a pointer to the DirectoryEntry with the given Name, or NULL (0) if there is no such entry
DirectoryEntry* find(string const& Name);
//Removes the directory entry with the given Name.
void remove(string const& Name);
private:
//Pointer to the first element in the directory entry linked list
DirectoryEntry *pDirectoryEntry;
};
#endif
|
Here is the directoryentry.h header file (they provided):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#ifndef DIRECTORY_ENTRY_H
#define DIRECTORY_ENTRY_H
#include <string>
using namespace std;
class DirectoryEntry{
public:
//Constructor
DirectoryEntry( string const& name, string const& number );
//Destructor
~DirectoryEntry();
//Getters
string getName();
string getNumber();
//The pointer to the next element in the linked list
DirectoryEntry * next;
private:
//Person's name
string name;
//Person's phone number
string number;
};
#endif
|
Here is my directoryofentry.cpp file from the last assignment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include "stdafx.h"
#include "directoryentry.h"
#include "string"
using namespace std;
string DirectoryEntry::getName()
{
return name;
}
string DirectoryEntry::getNumber()
{
return number;
}
|
Here is the phonebook.h header file (they provided):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <string>
#include "directoryentrieslist.h"
using namespace std;
class Phonebook{
public:
//Constructor
Phonebook();
//Destructor
~Phonebook();
//Loads the data from the given filename into the private DirectoryEntriesList.
void loadData(string const& filename);
//Returns the number associated with 'name'.
string lookupName( string const& name );
//If 'name' already exists in the directory, change his number to 'number'. Otherwise, create a new directory entry with the given 'name' and 'number'. Return true iff the 'name' already exists.
bool addChangeEntry(string const& name, string const& number );
//If 'name' exists in the directory, remove his name/number. Return true iff 'name' already exists.
bool removeEntry(string const& name );
//Write all changes back to the file that we opened from.
bool save();
//Print all names/numbers in the directory.
void printAll();
private:
//Returns the index in 'directoryEntries' where the entry with name 'name' exists. Returns -1 if it cannot be found. Use this function when implementing some of the public functions.
int findName( string const& name );
//Our linked list of directory entries.
DirectoryEntriesList directoryEntries;
//Current number of entries in the phone book
int numEntries;
//Where our data was loaded from.
string sourceFilename;
};
#endif
|
Here is my phonebook.cpp file from the last assignment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include "fstream"
#include "stdafx.h"
#include <cstdlib>
#include "string"
#include "phonebook.h"
#include "directoryentry.h"
using namespace std;
int l = 0;
void Phonebook::loadData(string const& filename)
{
string line;
ifstream in_file;
in_file.open(filename);
sourceFilename = filename;
string s[200];
while (getline(in_file,line))
{
s[l] = line;
l++;
}
in_file.close();
numEntries = l/2;
for (int count=0; count<numEntries; count++)
{
this->directoryEntries[count] = new DirectoryEntry(s[count*2], s[count*2+1]);
}
}
string Phonebook::lookupName(string const& name)
{
for (int count=0; count<numEntries; count++)
{
if (directoryEntries[count]->getName() == name)
{
return directoryEntries[count] -> getNumber();
}
}
return "THIS NAME DOES NOT EXIST!";
}
//If 'name' already exists in the directory, change his number to 'number'. Otherwise, create a new directory entry with the given 'name' and 'number'. Return true iff the 'name' already exists.
bool Phonebook::addChangeEntry(string const& name, string const& number)
{
for (int count=0; count<numEntries; count++)
{
if (directoryEntries[count]->getName() == name)
{
this -> directoryEntries[count] = new DirectoryEntry(name, number);
return true;
}
}
this -> directoryEntries[numEntries] = new DirectoryEntry(name, number);
numEntries++;
return false;
}
//If 'name' exists in the directory, remove his name/number. Return true iff 'name' already exists.
bool Phonebook::removeEntry(string const& name)
{
for (int count = 0; count<numEntries; count++)
{
if (directoryEntries[count]->getName() == name)
{
directoryEntries[count] = directoryEntries[count+1];
numEntries--;
return true;
}
}
return false;
}
//Write all changes back to the file that we opened from.
bool Phonebook::save()
{
ofstream out_file;
out_file.open(sourceFilename);
for (int count=0; count<numEntries; count++)
{
out_file << directoryEntries[count] -> getName() << endl;
out_file << directoryEntries[count] -> getNumber() << endl;
}
return true;
}
//Print all names/numbers in the directory.
void Phonebook::printAll()
{
ifstream in_file;
in_file.open(sourceFilename);
string line;
while (getline(in_file,line))
{
cout << line << endl;
}
}
|
I know all of it is lengthy, but I would be REALLY grateful if someone could please help me out! I could probably figure it out if I sat down and worked on it for long enough, but I'm on a time crunch and I have a lot of other exams to study for, so your help would go a long way for me.
Thank you so much!
- Miranda