Linked lists help

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

Join Date: Dec 2008
Posts: 23
Reputation: cbreeze has a little shameless behaviour in the past 
Solved Threads: 0
cbreeze cbreeze is offline Offline
Newbie Poster

Linked lists help

 
0
  #1
Mar 3rd, 2009
I am supposed to read in data from a file, create a linked list, and be able to traverse it. I feel like I have the first part of the code correct but I get a segmentation fault when I try and run the program and the pgm4.rpt file is empty when I open it.

here is the original assignment:

Write a complete C++ program that creates and queries a linked list of people who have served as United States presidents and/or vice presidents. Name your program file pgm4.cpp. Your program should do the following:
1. Define a struct called PublicOfficial to hold the following data about each president or vice president.
Name
State of birth
Office held (1 = President; 2 = Vice President; 3 = both President and Vice President
President Order (a number from 1 - 44 indicating whether he was the 1st, 2nd, 3rd, etc.)
Year first term as President began
Year last term as President ended
Vice President Order (a number from 1 - 44 indicating whether he was the 1st, 2nd, 3rd, etc.)
Year first term as Vice President began
Year last term as Vice President ended
Pointer to the next PublicOfficial in the list

2. In the main function, declare a pointer to serve as the head of a linked list of PublicOfficial structs. Also declare an ofstream variable and open a report file (pgm4.rpt) for writing out the results of queries about the data in the linked list.

3. Call a function named buildList, passing the head pointer by reference. This function will handle reading in all of the data from the file pgm4.dat and storing it in a linked list. First create the ifstream variable, open the input file and check for the success of that operation. Then read in the data for each public official until you reach the end of the file. For each person in the data file:
- Dynamically create a PublicOfficial node to store that person's data.
- Read in the person's Name, State of birth and Office held code. If the Office held code is 1, read in the next three lines and store the data in the President fields. Set the Vice President fields to zero. If the code is 2, read in the next three lines and store the data in the Vice President fields, setting the President fields to zero. If the code is 3 there will be six lines of data to read in, three with the President data followed by three with the Vice President data.
- Call a function named addOfficial to add the new official to the end of the linked list. Pass the pointer to the head of the list and the pointer to the new official to this function.
- Close the input file. All queries should be done using the linked list created from the data in this file.

4. Create a loop in the main function to get the user's selection from the menu of options shown below.

President/Vice President Queries
1. Presidents
2. Vice presidents
3. All who served in both offices
4. Presidents serving in 1700s
5. Presidents serving in 1800s
6. Presidents serving in 1900s
7. Presidents serving in 2000s
8. Presidents from a given state
9. Presidents serving less than full term
10. Exit program

heres the code so far:
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //**********structure to hold list****************************
  8.  
  9. struct publicOfficial
  10. {
  11. string name,
  12. sob;
  13. int officeHeld,
  14. prezOrder,
  15. prezStart,
  16. prezEnd,
  17. viceOrder,
  18. viceStart,
  19. viceEnd;
  20. publicOfficial* nextOffPtr;
  21. };
  22.  
  23. //**********************************
  24. //*********Function Prototypes******
  25. //**********************************
  26.  
  27. void buildList (publicOfficial* &);
  28. void addOfficial (publicOfficial* &, publicOfficial* &);
  29.  
  30. //**********************************
  31. //********Main Function*************
  32. //**********************************
  33.  
  34. int main ()
  35. {
  36.  
  37. publicOfficial* headOfficialPtr = NULL;
  38. publicOfficial* newOfficialPtr;
  39. newOfficialPtr = new publicOfficial;
  40.  
  41. ofstream resultdata;
  42. resultdata.open("pgm4.rpt");
  43.  
  44. //**********************************
  45. //*******Function Calls***************
  46. //**********************************
  47.  
  48. buildList (headOfficialPtr);
  49. }
  50. //**********************************
  51. //*******Function Definitions*******
  52. //**********************************
  53.  
  54. void buildList (publicOfficial* &headOfficialPtr) // BuildList will read in the data from the file
  55. {
  56. string testName;
  57.  
  58. ifstream numdata;
  59. numdata.open("pgm4.dat");
  60.  
  61. if ( !numdata ) // Check to make sure file was opened
  62. {
  63. cout << "Error opening file" << endl;
  64. }
  65. else
  66. {
  67. publicOfficial* newOfficialPtr;
  68.  
  69. while (!numdata.eof())
  70. {
  71. newOfficialPtr = new publicOfficial; // Create node to store data
  72.  
  73. getline(numdata, headOfficialPtr->name);
  74. getline(numdata, headOfficialPtr->sob);
  75. numdata >> headOfficialPtr->officeHeld;
  76.  
  77. if (headOfficialPtr->officeHeld == 1)
  78. {
  79. numdata >> headOfficialPtr->prezOrder;
  80. numdata >> headOfficialPtr->prezStart;
  81. numdata >> headOfficialPtr->prezEnd;
  82. headOfficialPtr->viceOrder = 0;
  83. headOfficialPtr->viceStart = 0;
  84. headOfficialPtr->viceEnd = 0;
  85. }
  86.  
  87. else if (headOfficialPtr->officeHeld == 2)
  88. {
  89. headOfficialPtr->prezOrder = 0;
  90. headOfficialPtr->prezStart = 0;
  91. headOfficialPtr->prezEnd = 0;
  92. numdata >> headOfficialPtr->viceOrder;
  93. numdata >> headOfficialPtr->viceStart;
  94. numdata >> headOfficialPtr->viceEnd;
  95. }
  96. else
  97. {
  98. numdata >> headOfficialPtr->prezOrder;
  99. numdata >> headOfficialPtr->prezStart;
  100. numdata >> headOfficialPtr->prezEnd;
  101. numdata >> headOfficialPtr->viceOrder;
  102. numdata >> headOfficialPtr->viceStart;
  103. numdata >> headOfficialPtr->viceEnd;
  104. }
  105.  
  106. addOfficial (headOfficialPtr, newOfficialPtr);
  107. numdata.ignore();
  108. getline(numdata, testName); // Test to see if the file is empty
  109. }
  110. }
  111. return;
  112. }
  113.  
  114. void addOfficial (publicOfficial* &headOfficialPtr, publicOfficial* &newOfficialPtr) // addOfficial will create new node for next official
  115. {
  116. publicOfficial* officialPtr;
  117.  
  118. if (!headOfficialPtr)
  119. {
  120. headOfficialPtr = newOfficialPtr;
  121. }
  122. else
  123. {
  124. officialPtr = headOfficialPtr;
  125. while (officialPtr->nextOffPtr)
  126.  
  127. officialPtr = officialPtr->nextOffPtr;
  128.  
  129. officialPtr->nextOffPtr = newOfficialPtr;
  130. }
  131. return;
  132. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Linked lists help

 
0
  #2
Mar 4th, 2009
When you read from the file, you're storing to a null object
  1. publicOfficial* newOfficialPtr;
  2.  
  3. while (!numdata.eof())
  4. {
  5. newOfficialPtr = new publicOfficial;
  6.  
  7. getline(numdata, headOfficialPtr->name);
  8. getline(numdata, headOfficialPtr->sob);
  9. numdata >> headOfficialPtr->officeHeld;
Why are you storing to headOfficialPtr->........ ?

Also, once you get that worked out, you'll find your loop control is going to not work quite right. If you're committed to using eof( ) test for the loop, you should (attempt to )read in a name before first entering the loop, and read name at the end of the loop (as yo do). That read at end of loop must be stored as the official's name when you start the next iteration. Kinda like:
  1. getline(numdata, testName);
  2. while (!numdata.eof())
  3. {
  4. newOfficialPtr = new publicOfficial;
  5.  
  6. headOfficialPtr->name = testName;
  7. getline(numdata, headOfficialPtr->sob);
  8. numdata >> headOfficialPtr->officeHeld;
  9.  
  10. //do the rest of the input stuff
  11.  
  12. getline(numdata, testName);
  13. }
But a better method, which only requires writing the initial getline once
  1. while ( getline(numdata, testName) )
  2. {
  3. //do all that other stuff
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC