| | |
passing linked lists through functions??
![]() |
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!
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!
C++ Syntax (Toggle Plain Text)
#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"); }
Last edited by Ancient Dragon; Oct 21st, 2007 at 7:04 am. Reason: add line numbers
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().
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)
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)
C++ Syntax (Toggle Plain Text)
while( inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp ) { // code here }
Last edited by Ancient Dragon; Oct 21st, 2007 at 7:09 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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!
(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!
![]() |
Similar Threads
- Linked Lists in C (C)
- Linked Lists Help C++ (C++)
- Linked Lists C++ Sorting Integers (C++)
- Stacks using doubly linked lists (C++)
- Looking up and displaying values in linked lists (C++)
- Concordance, Linked Lists and classes (C++)
- Singly-Linked Lists: Ultimate (C++)
- stack of linked lists (C++)
- Double Linked Lists and Functions required (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Random Numbers
- Next Thread: finding the lowest value
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






