My program compiles fine but I get a segmentation fault when reading in a dat file.... I had it in a loop but I thought thats what was causing the error, it doesnt even cout anything either. heres the code:

indata.open("lib/assign4.dat3");

cout << county[7];
indata >> county[8];
cout << county[8];
indata >> state[8];
indata >> date[8];
indata >> population[8];


indata >> county[9];
indata >> state[9];
indata >> date[9];
indata >> population[9];


indata >> county[10];
indata >> state[10];
indata >> date[10];
indata >> population[10];

indata.ignore();
getline(indata, name[11]);
indata >> county[11];
indata >> state[11];
indata >> date[11];
indata >> population[11];

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

Is this your entire code..?? if so, it has some serious issues.. like missing header files.. and missing main( ) function...

if not, you should always check to see if your file was opened correctly.. could use if( instream_object.fail( ) ) cout << "file open failure!";

also.. I have a fealing that maybe some of the stuff you are reading in from the file might be exceeding the bounds of one or more of the cstring arrays.. (common segfault problem)

post all ye' code along with a sample file you are attempting to read from

Is this your entire code..?? if so, it has some serious issues.. like missing header files.. and missing main( ) function... also failure to create fstream objects..

I think it comes after this line
(*pos).printCities();
I did a cout after that lione and gout nothing, here is the full code...

/*Greg Airel
  CS 202
  Call# 73400
  Fall Semester
  Assignment 4
  11/6/05  */

//This program reads in data and puts them into an 
//objects.  They are then sorted in ascending order
//and put into a linked list.  Another data file is
//read in and new objects are instantiated and inserted
//into the linked list.  Objects with missing data
//prompt user to enter info. 

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip.h>

#include <fstream>

#include <list>

#include <vector> 

#include <string>

//  class definitions

class Cities
{
    public:

        Cities(string = "", string = "", string = "",
			   unsigned short int = 0, unsigned int = 0);

        void setCities(string,
                        string,
                        string,
                        unsigned short int,
                        unsigned int);

        void printCities();

        unsigned short int getdate();
		unsigned int getpop();

    private:

        string name;
        string county;
        string state;
        unsigned short int dateestab;
        unsigned int population;

}; /* end class Cities definition */


//function definitions

Cities::Cities(string cname, string ccounty, string cstate,
			   unsigned short int cdateestab, unsigned int cpopulation)
{
	name = cname;
	county = ccounty;
	state = cstate;
	dateestab = cdateestab;
	population = cpopulation;
	
}


void Cities::printCities()
{
    cout << "City Name = " << name << endl;
    cout << "County = " << county << endl;
    cout << "State = " << state << endl;
	cout << "Date Established = " << dateestab << endl;
	cout << "Population = " << population << endl << endl;
}


void Cities::setCities(string set_name, string set_county, string set_state,
						unsigned short int set_dateestab, unsigned int set_population)
{
	name = set_name;
	county = set_county;
	state = set_state;
	dateestab = set_dateestab;
	population = set_population;
}



unsigned short int Cities::getdate()
{

return dateestab;


} 

unsigned int Cities::getpop()
{

return population;


} 

//end function definitions

signed int main()



{
//declarations

	ifstream indata;

	unsigned short int i = 0;
	unsigned short int j = 0;
	unsigned short int z = 0;
	unsigned short int numitems = 0;


	string name[12];
	string county[12];
	unsigned short int date[12];
	unsigned int population[12];
	string state[12];
	string temp;

	list<Cities> llcities;
	list<Cities>::iterator pos;
	

//opens first data file

	indata.open("lib/assign4.dat1");

//reads in data and stores in array
	
	for(i = 0; i < 8; i++)
	{
		if (i == 1)
		{
			indata.ignore();
			getline(indata, name[i]);
		}
		else if (i == 6)
		{
			indata.ignore();
			getline(indata, name[i]);
		}
		else
		{
			indata >> name[i];
		}	
	}

//closes data file

	indata.close();

//sorts array of names in ascending order

numitems = 8;
    for (i = 0; i <= numitems - 2; i++)
        {
        for (j = 0; j <= numitems - i - 2; j++)
            {
            if (name[j] > name[j + 1])
                {
                temp = name[j];
                name[j] = name[j + 1];
                name[j + 1] = temp;
                }
            } 
        }

//opens second data file

	indata.open("lib/assign4.dat2");

//reads in data and stores in arrays
	
	for(i = 0; i < 8; i++)
	{
	indata >> county[i];
	indata >> state[i];
	indata >> date[i];
	indata >> population[i];
	}

//closes data file

	indata.close();

//instantiation of objects and adds to a linked list
	Cities object1(name[0], county[0], state[0], date[0], 
					population[0]);
	llcities.push_back(object1);

	Cities object2(name[1], county[1], state[1], date[1], 
					population[1]);
	llcities.push_back(object2);

	Cities object3(name[2], county[2], state[2], date[2], 
					population[2]);
	llcities.push_back(object3);

	Cities object4(name[3], county[3], state[3], date[3], 
					population[3]);
	llcities.push_back(object4);

	Cities object5(name[4], county[4], state[4], date[4], 
					population[4]);
	llcities.push_back(object5);

	Cities object6(name[5], county[5], state[5], date[5], 
					population[5]);
	llcities.push_back(object6);

	Cities object7(name[6], county[6], state[6], date[6], 
					population[6]);
	llcities.push_back(object7);

	Cities object8(name[7], county[7], state[7], date[7], 
					population[7]);
	llcities.push_back(object8);

//prints linked list
cout << "*****PRINTING SORTED CITIES*****" << endl << endl;
 	for (pos = llcities.begin(); pos != llcities.end(); pos++)
	{
		(*pos).printCities();
	}

	numitems = 12;

//opens the third data file

	indata.open("lib/assign4.dat3");

			cout << county[7];
			indata >> county[8];
			cout << county[8];
			indata >> state[8];
			indata >> date[8];
			indata >> population[8];


			indata >> county[9];
			indata >> state[9];
			indata >> date[9];
			indata >> population[9];
		

			indata >> county[10];
			indata >> state[10];
			indata >> date[10];
			indata >> population[10];
		
			indata.ignore();
			getline(indata, name[11]);
			indata >> county[11];
			indata >> state[11];
			indata >> date[11];
			indata >> population[11];
		



	i = 8;

	while (name[i] != "zzzzz")
	{


//determines what object number it is and instantiates, inserts 
//in correct position, and stores data read in
	if (i == 8)
	{
		Cities object9(name[8], county[8], state[8], date[8], 
					   population[8]);
	    llcities.push_back(object9);
		pos = llcities.begin();
		for (z = 0; z < numitems; z++)
		{
			pos++;

			if (name[i] > name[z])
			{
				llcities.insert(pos);
			}
		}
	}

	if (i == 9)
	{
		Cities object10(name[9], county[9], state[9], date[9], 
					   population[9]);
	    llcities.push_back(object10);
		pos = llcities.begin();
		for (z = 0; z < numitems; z++)
		{
			pos++;

			if (name[i] > name[z])
			{
				llcities.insert(pos);
			}
		}
	}

	if (i == 10)
	{
		Cities object11(name[10], county[10], state[10], date[10], 
					   population[10]);
	    llcities.push_back(object11);
		pos = llcities.begin();
		for (z = 0; z < numitems; z++)
		{
			pos++;

			if (name[i] > name[z])
			{
				llcities.insert(pos);
			}
		}
	}

	if (i == 11)
	{
		Cities object12(name[11], county[11], state[11], date[11], 
					   population[11]);
	    llcities.push_back(object12);
		pos = llcities.begin();
		for (z = 0; z < numitems; z++)
		{
			pos++;

			if (name[i] > name[z])
			{
				llcities.insert(pos);
			}
		}
	}
	i++;
	}

//closes third data file

   	indata.close();

	 //numitems = name.size;
	 //   for (i = 0; j <= numitems - 2; j++)
     //   {
     //   for (j = 0; k <= numitems - i - 2; k++)
     //       {
	//			pos++
	//			if (name[k] < name[k +1]);
	//			{
	//				llcities.insert
	//			}
    //        }  
    //    }

//moves position to end and moves to delete middle object

	pos = llcities.end();
	pos--;
	pos--;
	pos--;
	pos--;
	pos--;
	llcities.erase(pos);

//erases first object in linked list

	pos = llcities.begin();
	llcities.erase(pos);

//erases last object in linked list

	pos = llcities.end();
	llcities.erase(pos);

//prints objects in linked list

	for (pos = llcities.begin(); pos != llcities.end(); pos++)
	{
		(*pos).printCities();
	}
//loop sees which object is missing data and asks user to input
//data if needed
	i = 0;
	for (pos = llcities.begin(); pos != llcities.end(); pos++)
	{

		(*pos).getdate();
			if (date[i] == 0)
			{
				cin >> date[i];
			}
		(*pos).getpop();
			if (population[i] == 0)
			{
				cin >> population[i];
			}
		i = i +1;
	}

//prints objects in linked list

	for (pos = llcities.begin(); pos != llcities.end(); pos++)
	{
		(*pos).printCities();
	}
	return 0;
}

/*Greg Airel
  CS 202
  Call# 73400
  Fall Semester
  Assignment 4
  11/6/05  */

i just found out that the problem is with this line (*pos).printCities();

i still cont get it working...anyone?

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.