Ok, I posted earlier and your suggestions worked fine, I had to rework the program a bit but it compiles now. The only problem is that it segfaults right away now. This is the gdb backtrace:

(gdb) backtrace full
#0  0x95082630 in std::istream::sentry::sentry ()
No symbol table info available.
#1  0x95082d4b in std::istream::operator>> ()
No symbol table info available.
#2  0x0000281f in employee::getData ()
No symbol table info available.
#3  0x00002901 in main ()
No symbol table info available.

And here is my full code:

#include <iomanip>
#include <iostream>
#include <fstream>
#include "custom_sort.cpp"
using namespace std;

const double taxrate = .3;
const double overtime_rate = 1.5;
const int NUMEMP = 50;

class employee {
  // data members - all private.
  public:
  ifstream fin;
  int n;
  long int emp_id;
  char employee_type_flag;
  double avg_array[NUMEMP];
  public: double given_gross_pay;
  char fname[20],lname[20];
  double hours_worked, hourly_rate, gross_pay, net_pay, tax_amt, overtime_hours;
  virtual void calc_gross_pay()=0;
  void calc_net_pay() { tax_amt = gross_pay * taxrate;  net_pay = gross_pay - tax_amt;}
  void headers();
  void avg(double*);
  void getData();
  //fin>>emp_id>>fname>>lname>>hours_worked>>hourly_rate>>employee_type_flag>>given_gross_pay
  void setProperties(long int t_empid,char t_fname[20],char t_lname[20], double t_hoursworked, double t_hourlyrate, char t_emptypeflag, double t_givengross) { 
  	emp_id = t_empid;
  	*fname = *t_fname;
  	*lname = *t_lname;
  	hours_worked = t_hoursworked;
  	hourly_rate = t_hourlyrate;
  	employee_type_flag = t_emptypeflag;
  	given_gross_pay = t_givengross;
  } 
  // definitions later
  //constructor/destructor
  public:employee();
		~employee();
		void outputData();
}; // end employee class


class salary_employee: public employee { 
	//double hourly_rate = given_gross_pay / 52;
	//hourly_rate = hourly_rate/40;
	public:
	void calc_gross_pay() { 
		if (hours_worked > 40) { 
		overtime_hours = hours_worked - 40;
		gross_pay = (hours_worked * hourly_rate) + (overtime_hours * overtime_rate); } else 
		{
		gross_pay = (hours_worked * hourly_rate);overtime_hours = 0;
		} 	  
	}// end calc_gross_pay
	
	
}; // end salary_employee

class hourly_employee: public employee {
	public:
	void calc_gross_pay() { 
		if (hours_worked > 40) { overtime_hours = hours_worked - 40;
		gross_pay = (hours_worked * hourly_rate) + (overtime_hours * overtime_rate); } else 
		{
		gross_pay = (hours_worked * hourly_rate);overtime_hours = 0;
		} 
	}
	
}; // end hourly_employee

employee::employee() { fin.open("payroll.dat"); } 
employee::~employee() { fin.close(); };

void employee::avg(double *p) { 
  double total = 0;
  int count = 0;
  while(*p != 0) { 
    total = *p + total;
    count++;
    p++; } 
	  cout << endl << "Average net pay for all employees: " << (total/count) << endl;	
}; //end avg

void employee::headers() { 
    cout.flags(ios::left);
    cout << setw(150) << setfill('=') << "==Payroll Information" << endl; 
    cout << setfill(' ');
    cout << setw(20) <<  "ID" << setw(20) << "First Name" << setw(20) << "Last Name" << setw(20) << "Hours Worked" << setw(20) << "Overtime Hours" << setw(12) << "GrossPay" << setw(12) << "Tax Rate" << setw(12) << "Tax Amount" << setw(12) << "Net Pay"<< endl;
    cout << setw(150) << setfill('-')<< "" << endl;
    cout << setfill(' ');
  } 

void employee::getData() { 
  int i=0;
  employee *emp1[10];
  while(fin>>emp_id>>fname>>lname>>hours_worked>>hourly_rate>>employee_type_flag>>given_gross_pay) {
  if (employee_type_flag == 's') { 
  		emp1[i] = new salary_employee(); 
  	} else if (employee_type_flag == 'h') {
  		emp1[i] = new hourly_employee();
  } // end employee_type check
 		emp1[i]->setProperties(emp_id,fname,lname,hours_worked,hourly_rate,employee_type_flag,given_gross_pay);
		i++;

  employee::headers();
  emp1[i]->calc_gross_pay();
  emp1[i]->calc_net_pay();
  cout << setw(20) << emp1[i]->emp_id;
  cout << setw(20) << emp1[i]->fname; 
  cout << setw(20) << emp1[1]->lname;
  cout << setw(20) << setprecision(5) << hours_worked;
  cout << setw(20) << setprecision(5) << overtime_hours;
  cout << setw(12) << setprecision(6) << gross_pay;
  cout << setw(12) << setprecision(4) << taxrate; 
  cout << setw(12) << setprecision(4) << tax_amt;
  cout << setw(12) << setprecision(5) << net_pay;
  cout << endl;
  avg_array[i] = net_pay;
	i++;} //end while loop
	avg(avg_array);
//  
	
};  

 int main() { 
   employee *emp;
   emp->getData();
   // end employee_type check

   
   //
 	return 0;
 }

I really appreciate any help you could give me.

You increment i too early ...

emp1[i]->setProperties(emp_id,fname,lname,hours_worked,hourly_rate,employee_type_flag,given_gross_pay);

i++; // <- incremented too early
employee::headers();
// emp1[i] is not pointing to an allocated item at this point
emp1[i]->calc_gross_pay();

Note that you are not handling the case where employee_type_flag is something else than 's' or 'h'.

You increment i too early ...

emp1[i]->setProperties(emp_id,fname,lname,hours_worked,hourly_rate,employee_type_flag,given_gross_pay);

i++; // <- incremented too early
employee::headers();
// emp1[i] is not pointing to an allocated item at this point
emp1[i]->calc_gross_pay();

Note that you are not handling the case where employee_type_flag is something else than 's' or 'h'. There may be other errors too, I didn't look too closely, but fixing the getData() gets you started.

Ok, I fixed the increment but I'm still segfaulting... Ive commented out to see where its segfaulting. The decleration of *emp1[10] is the last line that works and gdb also crashes out at the file read...

the offending line being (the return is not in the code and is there for readability):

while(fin>>emp_id>>fname>>lname>>hours_worked>>hourly_rate>>
employee_type_flag>>given_gross_pay) {

Heres a repost of the new getData():

void employee::getData() { 
  int i=0;
  employee *emp1[10];
	while(fin>>emp_id>>fname>>lname>>hours_worked>>hourly_rate>>employee_type_flag>>given_gross_pay) {
 /* if (employee_type_flag == 's') { 
  		emp1[i] = new salary_employee(); 
  	} else if (employee_type_flag == 'h') {
  		emp1[i] = new hourly_employee();
  } else {emp1[i] = new hourly_employee(); } 
  
  // end employee_type check
 		emp1[i]->setProperties(emp_id,fname,lname,hours_worked,hourly_rate,employee_type_flag,given_gross_pay);

  employee::headers();
  emp1[i]->calc_gross_pay();
  emp1[i]->calc_net_pay();
  cout << setw(20) << emp1[i]->emp_id;
  cout << setw(20) << emp1[i]->fname; 
  cout << setw(20) << emp1[i]->lname;
  cout << setw(20) << setprecision(5) << hours_worked;
  cout << setw(20) << setprecision(5) << overtime_hours;
  cout << setw(12) << setprecision(6) << gross_pay;
  cout << setw(12) << setprecision(4) << taxrate; 
  cout << setw(12) << setprecision(4) << tax_amt;
  cout << setw(12) << setprecision(5) << net_pay;
  cout << endl;
  avg_array[i] = net_pay;
  
  */
  cout << "I is..." << i;
	i++;
	} //end while loop
	avg(avg_array);
//  
	
};

How about changing from

char fname[20], lname[20];

to

string fname; string lname;

Looking at the main() ...

int main() 
{ 
   employee *emp;

   emp = new employee; // this is missing ...

   emp->getData();
...

Changed from char array to string...no go.

I can't do new employee() because employee is an abstract class. Its only there to be a base for salary_employee and hourly_employee. But the problem is I need to check a flag from the text file to see if the employee is a hourly or salaried and then initialize the array index with the correct employee type.

I thought "employee *emp;" was synonymous with *emp = new employee();

Edit: SOLVED.

After looking at my own post, I tried not making employee abstract (by making the "calc_gross_pay" not virtual and just overloaded the virtual function instead. That seems to have done the trick. I would still be interested in how to do it the other way though if anyone has another solution.

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.