8 Replies - 509 Views - Last Post: 27 June 2012 - 12:19 PM Rate Topic: -----

#1 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Import/Export

Posted 26 June 2012 - 11:14 PM

I was thinking I had this one done. Apparently I was way off the mark. Below is the code I've ended up with, but I have in no way accomplished the project. Exhausted and fed up, will try to tackle again tomorrow.


Project:
Assume there are two input files for a payroll program. The first contains the employee’s name (a string), id number (an integer) an hourly rate (a real number). The second contains the employee’s id number and hours worked (a real number). The records in the first file are ordered by name and the records in the second file are ordered by id number. If a person did not work during the current time period, there will be no record in the second file for that person. Write a file of payroll records to an output file for all employees. Each record should contain the employee name followed by # followed by the hours worked, hourly rate, and pay amount. Use a space as a separator between items in the output file. There should be a record in the output file for every person including those who did not work in that pay period.

Files given:

employees.txt
Andrea 1541 7.27
Barry 3794 9.64
Chantal 6260 9.39
Dorian 4740 9.20
Erin 7692 9.18
Fernand 4360 7.14
Gabrielle 2442 8.74
Humberto 5669 9.39
Ingrid 6512 9.16
Jerry 5255 8.08

hours.txt
1541 47
6260 36
4740 37
7692 23
4360 34
2442 25
5669 45
5255 49


#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "pay.txt"		//payroll file

//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	processEmp(eds,
		hrds,
		pds);

	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}

void processEmp
	(ifstream& eds,
	ifstream& hrds,
	ofstream& pds)
{
	string name;		//input: employee name from inFileEmp
	int id1;		//input: employee id from inFileEmp
	float rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	float pay;		//output: pay
	int noHours = 0;
		
	
	hrds >> id2 >> hours;
	while (!hrds.eof())
	{
		eds >> name >> id1 >> rate;
		pay = rate * hours;
		if (id1 == id2)
		{
			pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
			hrds >> id2 >> hours;
		}
		else if (id1 != id2)
		{
			eds >> name >> id1 >> rate;
		}
		else (eds.eof());
		{
			pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
			hrds >> id2 >> hours;
		}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Import/Export

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Import/Export

Posted 27 June 2012 - 12:04 AM

Try something like
while ( hrds >> id2 >> hours ) {
  while ( eds >> name >> id1 >> rate ) {
    if ( id2 == id1 ) {
    }
  }
  eds.seekg (0, ios::beg); // rewind back to start of file
  eds.clear();             // reset any error flags
}


Since you scan the employee file repeatedly, you need to rewind at the end of each scan.
Was This Post Helpful? 1
  • +
  • -

#3 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Import/Export

Posted 27 June 2012 - 09:09 AM

Okay, I tried (I think) to implement what you've said. Below is what I have, but I'm stuck in a loop.

#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "pay.txt"		//payroll file

//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	processEmp(eds,
		hrds,
		pds);

	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}

void processEmp
	(ifstream& eds,
	ifstream& hrds,
	ofstream& pds)
{
	string name;		//input: employee name from inFileEmp
	int id1;		//input: employee id from inFileEmp
	float rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	float pay;		//output: pay
	int flag;
	flag = 0;
	int noHours = 0;
	
	
	eds >> name >> id1 >> rate;
	while (!(eds.eof()))
	{
		pay = rate * hours;
		hrds >> id2 >> hours;
			
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  	hrds.clear();  
		eds >> name >> id1 >> rate;
		  
	}
	else if (id1 != id2)
	{
		while ((id1 !=id2) && (!(hrds.eof())) && (flag != 1))
		{
			hrds >> id2 >> hours;
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear();  
				eds >> name >> id1 >> rate;
			}
			else if ((id1 != id2) && (hrds.eof()))
			{
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear(); 
				flag = 1; 
			}
			else
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
				eds >> name >> id1 >> rate;
			}
			
		}
	}
	}
	flag = 0;
}



View PostSalem_c, on 27 June 2012 - 12:04 AM, said:

Try something like
while ( hrds >> id2 >> hours ) {
  while ( eds >> name >> id1 >> rate ) {
    if ( id2 == id1 ) {
    }
  }
  eds.seekg (0, ios::beg); // rewind back to start of file
  eds.clear();             // reset any error flags
}


Since you scan the employee file repeatedly, you need to rewind at the end of each scan.

Was This Post Helpful? 0
  • +
  • -

#4 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Import/Export

Posted 27 June 2012 - 10:11 AM

This is where I'm at so far, using the clearing methods.

#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "pay.txt"		//payroll file

//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	processEmp(eds,
		hrds,
		pds);

	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}

void processEmp
	(ifstream& eds,
	ifstream& hrds,
	ofstream& pds)
{
	string name;		//input: employee name from inFileEmp
	int id1;		//input: employee id from inFileEmp
	float rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	float pay;		//output: pay
	int flag;
	flag = 0;
	int noHours = 0;
	
	
	eds >> name >> id1 >> rate;
	while (!(eds.eof()))
	{
		pay = rate * hours;
		hrds >> id2 >> hours;
			
	if (id1 == id2)
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
		eds >> name >> id1 >> rate;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  	hrds.clear();  
		  
	}
	else if ((id1 != id2) && (hrds.eof()) && (flag = 2))
	{
		pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
		eds >> name >> id1 >> rate;
		hrds.seekg (0, ios::beg); 		// rewind back to start of file
		hrds.clear(); 
		flag=0;
	}
	else if ((id1 != id2) && (flag !=2))
	{
		while ((id1 !=id2) && (!(hrds.eof())))
		{
			hrds >> id2 >> hours;
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
				eds >> name >> id1 >> rate;
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear();  
			}
			else if ((id1 != id2) && (hrds.eof()) && (flag < 2))
			{
				hrds.seekg (0, ios::beg); 		// rewind back to start of file
	  			hrds.clear(); 
				flag++;
			}
			else if (id1 != id2)
			{
				hrds >> id2 >> hours;
			}
		}
	}
	
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,282
  • Joined: 25-December 09

Re: Import/Export

Posted 27 June 2012 - 10:27 AM

Do you have a question or problem with your code or is it working properly?

I myself would suggest that instead of continually reading the file consider reading the files once, and placing the contents into vectors/arrays of structures, to make searching for matches faster and easier.


Jim

This post has been edited by jimblumberg: 27 June 2012 - 10:28 AM

Was This Post Helpful? 1
  • +
  • -

#6 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Import/Export

Posted 27 June 2012 - 11:06 AM

Yes, the last iteration of my code returns all the pays as zero except for the last one. I must be missing a loop here somewhere. The reason I'm not doing as you suggested (though that sounds much better) is I'm new to coding. This is my eighth code. I'm taking an online class, and we haven't gotten to the point of learning those sort of things yet.
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Import/Export

Posted 27 June 2012 - 11:10 AM

Why are you still using eof() all over the place?

eof() doesn't do what you think it should
http://sourceforge.n....php?title=Feof
End of file can only become true when some input action (say fin >> var) has FAILED.

So you always have to check the stream state as part of reading the file anyway, which is what my code does.
Was This Post Helpful? 1
  • +
  • -

#8 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Import/Export

Posted 27 June 2012 - 11:20 AM

I used it only because that's how they said to use it in the book. I should insert your code after the int hours and get rid of all the rest?
Was This Post Helpful? 0
  • +
  • -

#9 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Import/Export

Posted 27 June 2012 - 12:19 PM

Thanks to your help, I ended up with this.

#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "paySimpleOut.txt"	//payroll file

//Functions used
void processEmp (ifstream&, ifstream&, ofstream&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	processEmp(eds,
		hrds,
		pds);

	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}

void processEmp
	(ifstream& eds,
	ifstream& hrds,
	ofstream& pds)
{
	string name;		//input: employee name from inFileEmp
	int id1;		//input: employee id from inFileEmp
	double rate;		//input: employee pay rate from inFileEmp
	int id2;		//input: employee id from inFileHour
	int hours;		//input: employee hours worked from inFileHour
	double pay;		//output: pay
	int noHours;
	noHours = 0;

	
	
	id2 = 0;
	eds >> name >> id1 >> rate;
	while (!eds.eof())
	{
		hrds.clear();
		hrds.seekg (0, ios::beg);
		while (!hrds.eof() && id1 != id2)
		{
			hrds >> id2 >> hours;
		}
			if (id1 == id2)
			{
				pds << name << " " << id1 << " " << rate << " " << hours << " $" << rate * hours << endl;
			}
			else
			{
				pds << name << " " << id1 << " " << rate << " " << noHours << " $" << rate * noHours << endl;
			}
		eds >> name >> id1 >> rate;
	}
}



The only thing that doesn't seem right is that it's one continuous line. It does have what I believe is a break char though. Any tips? Also, in case you were curious, I ended up with this version from help from another forum.

#include <fstream>		//required for file streams
#include <iostream>
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <map>
#include <string>
#include <string.h>

using namespace std;

#define inFileEmp "employees.txt"	//employee file
#define inFileHour "hours.txt"		//hour file
#define outFile "payMapOut.txt"		//payroll file

//Functions used
void processHours(std::istream&, std::map<int, double>&);	//process all employees and display name, pay, etc

int main()
{
	ifstream eds;		//input: employee data stream
	ifstream hrds;		//input: hour data stream
	ofstream pds;		//output: all info per employee
	int id;
	double rate;
	double hours;
	string name;

	//Prepare files
	eds.open(inFileEmp);
	if (eds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileEmp << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	hrds.open(inFileHour);
	if (hrds.fail())
	{
		cerr << "*** ERROR: Cannot open " << inFileHour << " for input." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	
	pds.open(outFile);
	if (pds.fail())
	{
		cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl;
		return EXIT_FAILURE;	//failure return
	}
	
	std::map<int, double> aIdToHours;
   	processHours(hrds, aIdToHours);

	
    	while(eds >> name >> id >> rate)
    	{
     		hours = aIdToHours[id];
     	 	pds << name << " " << id << " " << rate << " " << hours << " $" << hours*rate << std::endl;
    	}

   	
	//Close files
	eds.close();
	hrds.close();
	pds.close();
	
	return 0;
}



void processHours(std::istream& hrds, std::map<int, double>& oData)
{
 	 int id;
 	 double hours;
 	 while(hrds >> id >> hours)
  	{
   		 oData[id] = hours;
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1