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");   
}

The problem is in Read_File, you have to pass listTop pointer by reference so that Read_File() can change the value of the pointer that was created in main().

void Read_File(resident *& listTop, int &residentCount)

Next problem with that function -- delete the declaration of listTop at line 47 because it is a duplicate definition of the parameter and your compiler should have given you an error about that.


The loop at line 67 is constructed wrong -- it should be a while loop, not a do loop (which means you can delete the if statement on line 71)

while( inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp )
{
   // code here
}

Thanks Ancient Dragon,

(I will number my lines from now on!)...

I changed the pointer to be passed as a reference. That definitely helped read in the data from the file. My output still isn't working, but I had to do a mini-display elsewhere in my program that worked. So, I just need to take another look at my display function now. I will try and figure that one out today though. Thanks again for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.