Hello,
I am trying to call two functions: one that pulls data from a file and stores into a linked list, and one that displays this data to the screen. I pulled my code out of the functions and created a new file and included everything in the main function, and everything worked (the reading in of data and displaying to screen). Sooo...that led me to believe that the problem lies in passing the linked list between the functions. Any idea what I could be doing wrong?? (I left out the other functions in the program for now). Thanks in advance for your help!
#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
int main()
{
Read_File(listTop, residentCount);
Process_Menu_Choice(menuChoice, listTop, residentCount);
}
void Read_File(resident *listTop, int &residentCount)
{
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
}
void Process_Menu_Choice(char menuChoice, resident *listTop,
int &residentCount)
{
cout << endl;
while (menuChoice != 'e')
{
menu(menuChoice);
if (menuChoice == 's')
Display_Residents(listTop, residentCount);
}
}
void Display_Residents(resident *listTop, int &residentCount)
{
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");
}