passing linked lists through functions??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

passing linked lists through functions??

 
0
  #1
Oct 20th, 2007
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!

  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <cctype>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. enum phoneNumType {work, home, cell, };
  12.  
  13. struct resident{
  14. string ssn; // social security number
  15. char firstInitial; // first initial of resident
  16. string lastName; // last name of resident
  17. string phoneNum; // phone number of resident
  18. phoneNumType type; // phone number type (cell, home, or work)
  19. resident* next; // points to next node
  20. };
  21.  
  22. const int MAX_NUM_RESIDENTS = 500;
  23.  
  24. const string RES_FILENAME = "RESIDENTS.TXT";
  25.  
  26. // PROTOTYPES OF FUNCTIONS LISTED HERE
  27.  
  28. [COLOR="red"]int main()[/COLOR]
  29. {
  30.  
  31.  
  32. [COLOR="red"] Read_File(listTop, residentCount);
  33. Process_Menu_Choice(menuChoice, listTop, residentCount);[/COLOR]
  34.  
  35.  
  36. }
  37.  
  38. [COLOR="red"]void Read_File(resident *listTop, int &residentCount)[/COLOR]
  39. {
  40.  
  41.  
  42. residentCount = 0; // number of residents in array of records
  43.  
  44. int newAdd; // determines whether user is adding new resident at beginning
  45. // of file or from main menu
  46.  
  47. fstream inFile;
  48. resident *oneResident,
  49. *listTop = NULL,
  50. *listBottom = NULL;
  51.  
  52. // Following are temp variables that hold values while being read in from file
  53. char temp,
  54. oneFirstInitial,
  55. pauseChar;
  56. string oneLastName,
  57. onePhoneNum,
  58. oneSsn;
  59.  
  60. inFile.open(RES_FILENAME.c_str());
  61.  
  62. if (!inFile)
  63. cout << "Error opening data file" << endl;
  64.  
  65. else { // Data file opened successfully
  66.  
  67. do
  68. {
  69. inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp;
  70.  
  71. if (inFile)
  72. {
  73.  
  74. oneResident = new (nothrow) resident;
  75.  
  76. if (oneResident == NULL)
  77. {
  78. cout << "Heap error - could not allocate memory" << endl;
  79. cin >> pauseChar;
  80. }
  81.  
  82. else
  83. {
  84.  
  85. oneResident->ssn = oneSsn;
  86. oneResident->firstInitial = oneFirstInitial;
  87. oneResident->lastName = oneLastName;
  88. oneResident->phoneNum = onePhoneNum;
  89.  
  90.  
  91. if (temp == 'W')
  92. oneResident->type = work;
  93. else if (temp == 'H')
  94. oneResident->type = home;
  95. else if (temp == 'C')
  96. oneResident->type = cell;
  97.  
  98. oneResident->next = NULL;
  99.  
  100. }
  101.  
  102. if (listTop == NULL)
  103. {
  104. listBottom = oneResident;
  105. listTop = oneResident;
  106. }
  107.  
  108. else
  109. {
  110. listBottom->next = oneResident;
  111. listBottom = oneResident;
  112. }
  113.  
  114. } // end of if
  115.  
  116. residentCount++;
  117.  
  118. } while ( inFile && (oneResident != NULL) );
  119.  
  120. } // ends else
  121.  
  122. }
  123.  
  124. [COLOR="red"]void Process_Menu_Choice(char menuChoice, resident *listTop,
  125. int &residentCount)[/COLOR]
  126. {
  127. cout << endl;
  128.  
  129. while (menuChoice != 'e')
  130. {
  131. menu(menuChoice);
  132.  
  133. if (menuChoice == 's')
  134. [COLOR="red"]Display_Residents(listTop, residentCount);[/COLOR]
  135.  
  136.  
  137. }
  138.  
  139. }
  140.  
  141.  
  142. [COLOR="red"]void Display_Residents(resident *listTop, int &residentCount)[/COLOR]
  143. {
  144. listTop = NULL;
  145. int test = 0;
  146. resident *oneResident;
  147.  
  148. cout << endl << setw(5) << "Name" << setw(18) << "SSN" << setw(15)
  149. << "Phone" << setw(14) << "Type" << endl
  150. << "----------------- ----------- ------------- ------"
  151. << endl << endl;
  152.  
  153. oneResident = listTop;
  154.  
  155. while (oneResident !=NULL)
  156. {
  157.  
  158. cout << oneResident->firstInitial << " " << setw(17) << left
  159. << oneResident->lastName << setw(10) << right
  160. << oneResident->ssn << setw(15)
  161. << oneResident->phoneNum << " ";
  162.  
  163. if (oneResident->type == work)
  164. cout << "Work";
  165. else if (oneResident->type == home)
  166. cout << "Home";
  167. else if (oneResident->type == cell)
  168. cout << "Cell";
  169.  
  170. cout << endl;
  171.  
  172. oneResident = oneResident->next;
  173.  
  174. }
  175.  
  176. cout << endl;
  177. system ("PAUSE");
  178. }
Last edited by Ancient Dragon; Oct 21st, 2007 at 7:04 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1457
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: passing linked lists through functions??

 
0
  #2
Oct 21st, 2007
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)
  1. while( inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp )
  2. {
  3. // code here
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

Re: passing linked lists through functions??

 
0
  #3
Oct 21st, 2007
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC