#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
enum phoneNumType {work, home, cell, };
struct resident{
string ssn; // social security number
char firstInitial; // first initial of resident
string lastName; // last name of resident
string phoneNum; // phone number of resident
phoneNumType type; // phone number type (cell, home, or work)
resident* next; // points to next node
};
const int MAX_NUM_RESIDENTS = 500;
const string RES_FILENAME = "RESIDENTS.TXT";
// PROTOTYPES OF FUNCTIONS LISTED HERE
[COLOR="red"]int main()[/COLOR]
{
[COLOR="red"] Read_File(listTop, residentCount);
Process_Menu_Choice(menuChoice, listTop, residentCount);[/COLOR]
}
[COLOR="red"]void Read_File(resident *listTop, int &residentCount)[/COLOR]
{
residentCount = 0; // number of residents in array of records
int newAdd; // determines whether user is adding new resident at beginning
// of file or from main menu
fstream inFile;
resident *oneResident,
*listTop = NULL,
*listBottom = NULL;
// Following are temp variables that hold values while being read in from file
char temp,
oneFirstInitial,
pauseChar;
string oneLastName,
onePhoneNum,
oneSsn;
inFile.open(RES_FILENAME.c_str());
if (!inFile)
cout << "Error opening data file" << endl;
else { // Data file opened successfully
do
{
inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp;
if (inFile)
{
oneResident = new (nothrow) resident;
if (oneResident == NULL)
{
cout << "Heap error - could not allocate memory" << endl;
cin >> pauseChar;
}
else
{
oneResident->ssn = oneSsn;
oneResident->firstInitial = oneFirstInitial;
oneResident->lastName = oneLastName;
oneResident->phoneNum = onePhoneNum;
if (temp == 'W')
oneResident->type = work;
else if (temp == 'H')
oneResident->type = home;
else if (temp == 'C')
oneResident->type = cell;
oneResident->next = NULL;
}
if (listTop == NULL)
{
listBottom = oneResident;
listTop = oneResident;
}
else
{
listBottom->next = oneResident;
listBottom = oneResident;
}
} // end of if
residentCount++;
} while ( inFile && (oneResident != NULL) );
} // ends else
}
[COLOR="red"]void Process_Menu_Choice(char menuChoice, resident *listTop,
int &residentCount)[/COLOR]
{
cout << endl;
while (menuChoice != 'e')
{
menu(menuChoice);
if (menuChoice == 's')
[COLOR="red"]Display_Residents(listTop, residentCount);[/COLOR]
}
}
[COLOR="red"]void Display_Residents(resident *listTop, int &residentCount)[/COLOR]
{
listTop = NULL;
int test = 0;
resident *oneResident;
cout << endl << setw(5) << "Name" << setw(18) << "SSN" << setw(15)
<< "Phone" << setw(14) << "Type" << endl
<< "----------------- ----------- ------------- ------"
<< endl << endl;
oneResident = listTop;
while (oneResident !=NULL)
{
cout << oneResident->firstInitial << " " << setw(17) << left
<< oneResident->lastName << setw(10) << right
<< oneResident->ssn << setw(15)
<< oneResident->phoneNum << " ";
if (oneResident->type == work)
cout << "Work";
else if (oneResident->type == home)
cout << "Home";
else if (oneResident->type == cell)
cout << "Cell";
cout << endl;
oneResident = oneResident->next;
}
cout << endl;
system ("PAUSE");
}