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:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

//**********structure to hold list****************************

struct publicOfficial
{
        string name,
        sob;
        int officeHeld,
        prezOrder,
        prezStart,
        prezEnd,
        viceOrder,
        viceStart,
        viceEnd;
        publicOfficial* nextOffPtr;
};

//**********************************
//*********Function Prototypes******
//**********************************

void buildList (publicOfficial* &);
void addOfficial (publicOfficial* &, publicOfficial* &);

//**********************************
//********Main Function*************
//**********************************

int main ()
{

publicOfficial* headOfficialPtr = NULL;
publicOfficial* newOfficialPtr;
newOfficialPtr = new publicOfficial;

    ofstream resultdata;
    resultdata.open("pgm4.rpt");

//**********************************
//*******Function Calls***************
//**********************************

                buildList (headOfficialPtr);
}
//**********************************
//*******Function Definitions*******
//**********************************

void buildList (publicOfficial* &headOfficialPtr)                          // BuildList will read in the data from the file
{
    string testName;

    ifstream numdata;
    numdata.open("pgm4.dat");

    if ( !numdata )                                                       // Check to make sure file was opened
    {
        cout << "Error opening file" << endl;
    }
        else
        {
                publicOfficial* newOfficialPtr;

                while (!numdata.eof())
                {
                        newOfficialPtr = new publicOfficial;                                         // Create node to store data

                        getline(numdata, headOfficialPtr->name);
                        getline(numdata, headOfficialPtr->sob);
                        numdata >>  headOfficialPtr->officeHeld;

                                if (headOfficialPtr->officeHeld == 1)
                                {
                                        numdata >> headOfficialPtr->prezOrder;
                                        numdata >> headOfficialPtr->prezStart;
                                        numdata >> headOfficialPtr->prezEnd;
                                        headOfficialPtr->viceOrder = 0;
                                        headOfficialPtr->viceStart = 0;
                                        headOfficialPtr->viceEnd = 0;
                                }

                                        else if (headOfficialPtr->officeHeld == 2)
                                        {
                                                headOfficialPtr->prezOrder = 0;
                                                headOfficialPtr->prezStart = 0;
                                                headOfficialPtr->prezEnd = 0;
                                                numdata >> headOfficialPtr->viceOrder;
                                                numdata >> headOfficialPtr->viceStart;
                                                numdata >> headOfficialPtr->viceEnd;
                                        }
                                                else
                                                {
                                                        numdata >> headOfficialPtr->prezOrder;
                                                        numdata >> headOfficialPtr->prezStart;
                                                        numdata >> headOfficialPtr->prezEnd;
                                                        numdata >> headOfficialPtr->viceOrder;
                                                        numdata >> headOfficialPtr->viceStart;
                                                        numdata >> headOfficialPtr->viceEnd;
                                                }

        addOfficial (headOfficialPtr, newOfficialPtr);
        numdata.ignore();
        getline(numdata, testName);                                                  // Test to see if the file is empty
        }
}
        return;
}

void addOfficial (publicOfficial* &headOfficialPtr, publicOfficial* &newOfficialPtr)  // addOfficial will create new node for next official
{
        publicOfficial* officialPtr;

        if (!headOfficialPtr)
        {
                headOfficialPtr = newOfficialPtr;
        }
                else
                {
                        officialPtr = headOfficialPtr;
                        while (officialPtr->nextOffPtr)

                                officialPtr = officialPtr->nextOffPtr;

                        officialPtr->nextOffPtr = newOfficialPtr;
                }
        return;
}

When you read from the file, you're storing to a null object

publicOfficial* newOfficialPtr;

		while (!numdata.eof())
		{
			newOfficialPtr = new publicOfficial;

			getline(numdata, headOfficialPtr->name);
			getline(numdata, headOfficialPtr->sob);
			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:

getline(numdata, testName);   
		while (!numdata.eof())
		{
			newOfficialPtr = new publicOfficial;  

			headOfficialPtr->name = testName;
			getline(numdata, headOfficialPtr->sob);
			numdata >>  headOfficialPtr->officeHeld;

                        //do the rest of the input stuff

			getline(numdata, testName);
		}

But a better method, which only requires writing the initial getline once

while ( getline(numdata, testName) )
		{
                      //do all that other stuff
This article has been dead for over six months. Start a new discussion instead.