Need to sort an array of objects of type PhoneEntry, using PhoneEntry's greaterAlpha to do your comparisons. I have the sort function, but I'm not sure how to get everything working together.
#include <iostream>
#include <string>
using namespace std;
class PhoneNumber
{
private:
int area;
int prefix;
int suffix;
public:
istream& readNumber(istream&);
ostream& printNumber(ostream&) const;
};
//***************************************************************************
istream& PhoneNumber::readNumber(istream& fin)
{
return fin >> area >> prefix >> suffix;
}
//***************************************************************************
ostream& PhoneNumber::printNumber(ostream& fout) const
{
return fout << area << "-" << prefix << "-" << suffix;
}
//***************************************************************************
class PhoneEntry
{
private:
string lastName;
string firstName;
PhoneNumber phone;
ostream& _printDots(ostream&, int) const;
public:
greaterAlpha(PhoneEntry);
istream& readEntry(istream&);
ostream& printEntry(ostream&) const;
};
//***************************************************************************
ostream& PhoneEntry::_printDots(ostream& fout, int dots) const
{
if (dots % 2 == 1) // odd number of spaces, start with extra space
{
fout << " "; // start with 1 space.
dots--;
}
else
{
fout << " "; // even number of spaces, start with two spaces
dots -= 2;
}
while (dots > 0)
{
fout << ". ";
dots -= 2;
}
return fout;
}
//***************************************************************************
//
PhoneEntry::greaterAlpha(PhoneEntry entry)
{
void mySort(string array[], int size)
{
string temp;
int tempsub;
for (int i=0; i<size - 1; ++i)
{
temp = array[i];
tempsub = i;
for (int j = i + 1; j < size; ++j)
{
if (array[j] < temp)
{
temp = array[j];
tempsub = j;
}
}
array[tempsub] = array[i];
array[i] = temp;
}
}
}
//***************************************************************************
istream& PhoneEntry::readEntry(istream& fin)
{
fin >> lastName >> firstName;
if (fin)
phone.readNumber(fin);
return fin;
}
//***************************************************************************
ostream& PhoneEntry::printEntry(ostream& fout) const
{
fout << lastName << ", " << firstName;
_printDots(fout, 30 - lastName.size() - firstName.size() -2);
phone.printNumber(fout);
fout << endl;
return fout;
}
//***************************************************************************
// Repeatedly reads PhoneEntry's and prints formatted PhoneEntry's.
// Meant for input to be redirected from a file.
//
int main() {
const int MAX_COUNT = 500;
PhoneEntry directory[MAX_COUNT],
loadEntry;
int dirIndex(0),
dirCount;
while (loadEntry.readEntry(cin) )
{
if (dirIndex < MAX_COUNT)
{
directory[dirIndex++] = loadEntry;
}
else
{
cerr << "Over " << MAX_COUNT << " entries, exiting." << endl;
exit(1);
}
}
dirCount = dirIndex;
for (int i = 0; i < dirCount; ++i)
{
directory[i].printEntry(cout);
}
return 0;
}