I improved the code and expanded the database and fixed some stuff.
this will be the last post unless you wish me to respond.
//#define TEST
#include <string>
#include <iostream>
#include <fstream>
#include <list>
#include <iterator>
#include <sstream> //sorry, I thought it weas stringstream, but it's sstream, I am new to this really cool header
//stringstream turns string into a stream like iostream or like cin and cout or fstream.
//this lets you do REALLY COOL stuff.
using namespace std;
void _tospaces(string& s);
string spacesto_(string s);
class CEmployee {
public:
//can't initialize cars when you declare them in a class. it's similar to a struct.
int id ;
string lastName ,
firstName ,
middleName ,
address1 ,
address2 ,
city ,
stateProvince,
postalCode ,
country ,
department ,
mailstop ,
location ;
int hours ; //assume 24-hour hour, not 12-hour time format. did you mean hoursWorked?
double hourlyRate ; //when the next version of cC++ comes out past C++11, then we will have boost,
//which has a decimal data type. this would be preferable, since float and double have
//floating point errors repeatedly adding some numbers like .01
bool isBad;
//2 different constructors
CEmployee() {
id =0;
lastName ="";
firstName ="";
middleName ="";
address1 ="";
address2 ="";
city ="";
stateProvince ="";
postalCode ="";
country ="";
department ="";
mailstop ="";
location ="";
hours =0;
hourlyRate =0;
isBad=true;
}
CEmployee(
int _id ,
string _lastName ,
string _firstName ,
string _middleName ,
string _address1 ,
string _address2 ,
string _city ,
string _stateProvince,
string _postalCode ,
string _country ,
string _department ,
string _mailstop ,
string _location ,
int _hours ,
double _hourlyRate
) {
id =_id ;
lastName =_lastName ;
firstName =_firstName ;
middleName =_middleName ;
address1 =_address1 ;
address2 =_address2 ;
city =_city ;
stateProvince =_stateProvince ;
postalCode =_postalCode ;
country =_country ;
department =_department ;
mailstop =_mailstop ;
location =_location ;
hours =_hours ;
hourlyRate =_hourlyRate ;
isBad=false;
}
//destructor that doesn't do anything
~CEmployee() {}
void initialize() {
id =0;
lastName ="";
firstName ="";
middleName ="";
address1 ="";
address2 ="";
city ="";
stateProvince ="";
postalCode ="";
country ="";
department ="";
mailstop ="";
location ="";
hours =0;
hourlyRate =0;
isBad =true;
}
bool bad() {return isBad;}
void setBad() {isBad = true;}
void clearBad() {isBad = false;}
void setBad(bool _isBad) {isBad = _isBad;}
//property setting/getting functions
void setid(int _id) {id=_id;isBad=false;}
int getid() {return id;}
void setlastName(string _lastName) {lastName=_lastName;isBad=false;}
string getlastName() {return lastName;}
void setfirstName(string _firstName) {firstName=_firstName;isBad=false;}
string getfirstName() {return firstName;}
void setmiddleName(string _middleName) {middleName=_middleName;isBad=false;}
string getmiddleName() {return middleName;}
void setaddress1(string _address1) {address1=_address1;isBad=false;}
string getaddress1() {return address1;}
void setaddress2(string _address2) {address2=_address2;isBad=false;}
string getaddress2() {return address2;}
void setcity(string _city) {city=_city;isBad=false;}
string getcity() {return city;}
void setstateProvince(string _stateProvince) {stateProvince=_stateProvince;isBad=false;}
string getstateProvince() {return stateProvince;}
void setpostalCode(string _postalCode) {postalCode=_postalCode;isBad=false;}
string getpostalCode() {return postalCode;}
void setcountry(string _country) {country=_country;isBad=false;}
string getcountry() {return country;}
void setdepartment(string _department) {department=_department;isBad=false;}
string getdepartment() {return department;}
void setmailstop(string _mailstop) {mailstop=_mailstop;isBad=false;}
string getmailstop() {return mailstop;}
void setlocation(string _location) {location=_location;isBad=false;}
string getlocation() {return location;}
/* void set(string _) {=_;isBad=false;}
string get() {return ;}
void set(string _) {=_;isBad=false;}
string get() {return ;}
void set(string _) {=_;isBad=false;}
string get() {return ;}
void set(string _) {=_;isBad=false;}
string get() {return ;}
*/
void sethours(int _hours) {hours=_hours;isBad=false;}
int gethours() {return hours;}
void sethourlyRate(double _hourlyRate) {hourlyRate=_hourlyRate;isBad=false;}
double gethourlyRate() {return hourlyRate;}
//shift-in and shift-out I/O operators. must define as friends and not direct members.
friend istream& operator>>(istream& is, class CEmployee& ce) {
char line[128000+1];
if (is.good() && !is.eof()) {
do {
is.getline(line, 128000);
} while (is.good() && !is.fail() && !is.eof() && ('\0'==line[0] || '\n'==line[0]));
}
if (!is.good() || is.eof() || '\0'==line[0] || '\n'==line[0]) {
ce.initialize();
is.clear(ios_base::badbit|ios_base::failbit);
return is;
}
line[64999]='\0';
stringstream ss;
string sLine=line;
int id ;
string lastName ,
firstName ,
middleName ,
address1 ,
address2 ,
city ,
stateProvince,
postalCode ,
country ,
department ,
mailstop ,
location ;
int hours ;
double hourlyRate ;
ss.str(sLine); //convert line to stringstream
//input the record
if (ss.good()) {
ss >> id
>> lastName
>> firstName
>> middleName
>> address1
>> address2
>> city
>> stateProvince
>> postalCode
>> country
>> department
>> mailstop
>> location
>> hours
>> hourlyRate
;
if (!ss.fail()) {
// id
_tospaces(lastName );
_tospaces(firstName );
_tospaces(middleName );
_tospaces(address1 );
_tospaces(address2 );
_tospaces(city );
_tospaces(stateProvince);
_tospaces(postalCode );
_tospaces(country );
_tospaces(department );
_tospaces(mailstop );
_tospaces(location );
// hours
// hourlyRate
/*_tospaces();
_tospaces();
_tospaces();
_tospaces();
_tospaces();*/
//set the object's values using the interface provided
ce.setid (id );
ce.setlastName (lastName );
ce.setfirstName (firstName );
ce.setmiddleName (middleName );
ce.setaddress1 (address1 );
ce.setaddress2 (address2 );
ce.setcity (city );
ce.setstateProvince (stateProvince );
ce.setpostalCode (postalCode );
ce.setcountry (country );
ce.setdepartment (department );
ce.setmailstop (mailstop );
ce.setlocation (location );
ce.sethours (hours );
ce.sethourlyRate (hourlyRate );
/*ce.set ( );
ce.set ( );
ce.set ( );
ce.set ( );
ce.set ( );
ce.set ( );
ce.set ( );*/
} else {
ce.initialize();
is.clear(ios_base::badbit|ios_base::failbit);
}
} else {
ce.initialize();
is.clear(ios_base::badbit|ios_base::failbit);
}
return is;
}
friend ostream& operator<<(ostream& out, class CEmployee& ce) {
out << ce.getid () << " "
<< spacesto_(ce.getlastName ()) << " "
<< spacesto_(ce.getfirstName ()) << " "
<< spacesto_(ce.getmiddleName ()) << " "
<< spacesto_(ce.getaddress1 ()) << " "
<< spacesto_(ce.getaddress2 ()) << " "
<< spacesto_(ce.getcity ()) << " "
<< spacesto_(ce.getstateProvince ()) << " "
<< spacesto_(ce.getpostalCode ()) << " "
<< spacesto_(ce.getcountry ()) << " "
<< spacesto_(ce.getdepartment ()) << " "
<< spacesto_(ce.getmailstop ()) << " "
<< spacesto_(ce.getlocation ()) << " "
<< ce.gethours () << " "
<< ce.gethourlyRate () << endl;
return out;
}
void prettyprint(ostream& out) {
out << id << " "
<< lastName << " "
<< firstName << " "
<< middleName << " "
<< address1 << " "
<< address2 << " "
<< city << " "
<< stateProvince << " "
<< postalCode << " "
<< country << " "
<< department << " "
<< mailstop << " "
<< location << " "
<< hours << "hrs $";
// << hourlyRate << "hrs $";
out.width(3);
out.setf(ios::fixed);
out.precision(5);
out << hourlyRate;
out.setf(ios::floatfield);
out.precision(1);
out.width(1);
out << endl;
}
};
//this function strips _ out of strings.
//this allows strings that would normally have spaces in them to be coded with
//underscroes instead so that stringstream does not
void _tospaces(string& s) {
unsigned i;
string s2;
if (0 == s.compare(".")) {
s="";
return;
}
for (i=0; i < s.length(); i++) {
if ('_'==s[i]) {
s2 += ' '; //replace _ with a space
} else {
s2 += s[i]; //append the current character
}
}
s=s2; //assign string back to original
}
//this function converts spaces to _ in strings.
//this allows strings that would normally have spaces in them to be coded with
//underscores instead so that stringstream does not
//interpret the space as an argument separator like it normally does
//(it needs to be a string made of non-whitespace characters).
string spacesto_(string s) {
unsigned i;
if (0 == s.compare("")) {
return string(".");
}
for (i=0; i < s.length(); i++) {
if (' '==s[i]) {
s[i]='_'; //replace _ with a space
}
}
return s;
}
//sorting comparison function for .sort()
//you can make this a more complicated comparison function if you want to sort by
//employee name and then by id instead.
bool sortcomparebynamethenid(CEmployee first, CEmployee second) {
//sort by employee lastName,firstName,middleName and then by id
return first.getlastName() < second.getlastName()
&& first.getfirstName() < second.getfirstName()
&& first.getmiddleName() < second.getmiddleName()
&& first.getid() < second.getid();
}
bool sortcomparebyid(CEmployee first, CEmployee second) {
//sort by simple id comparison
return first.getid() < second.getid();
}
//just make sure you are working with a copy of your emplyee data file.
//either way, it's a good policy.
//this version reads, sorts, and writes.
//not sure why you are making this so complicated for something that does the same thing.
//implement e as an array or vector.
//why aren't you using .sort() ?
void sortRecById(char * filepath) {
fstream flatFileDatabase(filepath, ios_base::in|ios_base::out);
list<class CEmployee> employees; //let's be realistic, these is a collection of employees
list<class CEmployee>::iterator li;
CEmployee employeeRecord; //temporary record variable for reading data into collection
//counting lines
while(flatFileDatabase.good() && !flatFileDatabase.eof() && flatFileDatabase.fail()) {
flatFileDatabase >> employeeRecord;
if (!employeeRecord.bad()) {
employees.push_back(employeeRecord);
}
}
//it doesn't matter if you sort the employee numbers if they are disconnected from the record!
//then you just get a bunch of sorted record id's and the
//why don't you make id a member of e?
employees.sort(sortcomparebyid); //feed it the compare function we wrote
//rewrite the whole flat-file database
flatFileDatabase.clear();
flatFileDatabase.seekg(0, ios::beg); //reset get file pointer to start of file
flatFileDatabase.seekp(0, ios::beg); //reset put file pointer to start of file
flatFileDatabase.clear();
for(li = employees.begin(); li != employees.end(); li++) {
flatFileDatabase << *li; //output the CEmployee object pointed to by the iterator li
}
flatFileDatabase.close();
}
void sortRecByNameThenId(char * filepath) {
fstream flatFileDatabase(filepath, ios_base::in|ios_base::out);
list<class CEmployee> employees; //let's be realistic, these is a collection of employees
list<class CEmployee>::iterator li;
CEmployee employeeRecord; //temporary record variable for reading data into collection
//counting lines
while(flatFileDatabase.good() && !flatFileDatabase.eof() && flatFileDatabase.fail()) {
flatFileDatabase >> employeeRecord;
if (!employeeRecord.bad()) {
employees.push_back(employeeRecord);
}
}
//it doesn't matter if you sort the employee numbers if they are disconnected from the record!
//then you just get a bunch of sorted record id's and the
//why don't you make id a member of e?
employees.sort(sortcomparebynamethenid); //feed it the compare function we wrote
//rewrite the whole flat-file database
flatFileDatabase.clear();
flatFileDatabase.seekg(0, ios::beg); //reset get file pointer to start of file
flatFileDatabase.seekp(0, ios::beg); //reset put file pointer to start of file
flatFileDatabase.clear();
for(li = employees.begin(); li != employees.end(); li++) {
flatFileDatabase << *li; //output the CEmployee object pointed to by the iterator li
}
flatFileDatabase.close();
}
void show(char * filepath) {
fstream flatFileDatabase(filepath, ios_base::in);
CEmployee employeeRecord; //temporary record variable for reading data into collection
//counting lines
flatFileDatabase.seekg(0, ios::beg); //reset get file pointer to start of file
cout<<"-----Employee Records Start-----"<<endl;
while(flatFileDatabase.good()) {
flatFileDatabase >> employeeRecord;
if (!flatFileDatabase.bad()) {
employeeRecord.prettyprint(cout); //this is the friend function we defined in the class.
}
}
cout<<"-----Employee Records End-----"<<endl;
flatFileDatabase.close();
}
int main(void) {
#if defined(TEST)
//test sortcomparebyid
CEmployee c(
903,
"Farminn",
"Janieee",
"Whozitz",
"999 Zipp Rd",
"Rt 6",
"Anywhere",
"KY",
"98765-4321",
"USA",
"Administration",
"AD01",
"JL02",
38,
20.00
), d(
23,
"Palooka",
"Joe",
"Harva",
"123 blooky lane",
"",
"Blastoff",
"PA",
"12345-6789",
"USA",
"Shipping",
"MS-T12",
"DH71",
40,
9.5
);
cout<<sortcomparebyid(c,d);
cout<<sortcomparebyid(d,c)<<endl;
//test the CEmployee
cout<<c;
cout<<"id lastName firstName middleName address1 address2 city stateProvince postalCode country department mailstop location hours hourlyRate"<<endl;
cin>>c;
cout<<c;
#endif
//test sortRec
char flatFileDatabasepath[]="employee.txt";
cout<<"unsorted"<<endl;
show(flatFileDatabasepath);
sortRecById(flatFileDatabasepath);
cout<<"sorted by id"<<endl;
show(flatFileDatabasepath);
/*sortRecByNameThenId(flatFileDatabasepath);
cout<<"sorted by name then id"<<endl;
show(flatFileDatabasepath);*/
return 0;
}