GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jun-2012, 00:04
CoryMore CoryMore is offline
New Member
 
Join Date: Jun 2012
Posts: 1
CoryMore is on a distinguished road

Import/Export


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

CPP / C++ / C Code:
#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;
		}

	}
}
  #2  
Old 30-Jun-2012, 06:03
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 656
Mexican Bob is just really niceMexican Bob is just really niceMexican Bob is just really niceMexican Bob is just really nice

Re: Import/Export


Quote:
Originally Posted by CoryMore
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


Perhaps something like this?

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <map>

typedef struct st_payroll_record {
    std::string m_name;
    unsigned m_emp_id;
    float m_rate;
    float m_hours;
    st_payroll_record(std::string& name, unsigned emp_id, float rate, float hours) :
    m_name(name), m_emp_id(emp_id), m_rate(rate), m_hours(hours) {
    }
    friend std::ostream& operator<<(std::ostream& os, const st_payroll_record& rec) {
        os << rec.m_name << ' ' << rec.m_emp_id << ' ' <<
        std::setprecision(2) << std::fixed << rec.m_hours << 
        " $" << rec.m_rate << " $" << (rec.m_rate * rec.m_hours);
        return os;
    }
} RECORD;

typedef std::map<unsigned, RECORD> mapRecords;

void process_payroll_report(mapRecords& mrecs) {
    mapRecords::iterator it;
    for (it = mrecs.begin(); it != mrecs.end(); it++) {
        std::cout << it->second << std::endl;
    }
}

int main(void) {
    mapRecords mrecs;
    std::ifstream emp_file("employees.txt");
    std::ifstream hrs_file("hours.txt");

    if (emp_file.is_open()) {
        do {
            std::string name;
            emp_file >> name;
            unsigned emp_id;
            emp_file >> emp_id;
            float rate;
            emp_file >> rate;
            float hours = 0.00F;
            if (emp_file.good()) {
                RECORD rec(name, emp_id, rate, hours);
                mrecs.insert(std::pair<unsigned, RECORD>(emp_id, rec));
            }
        } while (emp_file.good());
        emp_file.close();
    }
    if (hrs_file.is_open()) {
        do {
            unsigned emp_id;
            float hours = 0.0F;
            hrs_file >> emp_id >> hours;
            if (hrs_file.good()) {
                mapRecords::iterator it = mrecs.find(emp_id);
                if (it != mrecs.end()) { // found
                    it->second.m_hours = hours; 
                }
            }
        } while (hrs_file.good());
        hrs_file.close();
    }
    process_payroll_report(mrecs);
    
    return 0;
}


Output:

Code:
Andrea 1541 47.00 $7.27 $341.69 Gabrielle 2442 25.00 $8.74 $218.50 Barry 3794 0.00 $9.64 $0.00 Fernand 4360 34.00 $7.14 $242.76 Dorian 4740 37.00 $9.20 $340.40 Jerry 5255 49.00 $8.08 $395.92 Humberto 5669 45.00 $9.39 $422.55 Chantal 6260 36.00 $9.39 $338.04 Ingrid 6512 0.00 $9.16 $0.00 Erin 7692 23.00 $9.18 $211.14

...wouldn't be terribly difficult to convert to writing to a file instead of to stdout.

With respect to the current political climate in the USA, of course we knew that "Barry" had no on-the-job hours; campaigning does take a lot of time away from the desk...


MxB
  #3  
Old 30-Jun-2012, 10:24
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,441
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Import/Export


Quote:
Originally Posted by Mexican Bob

With respect to the current political climate in the USA, of course we knew that "Barry" had no on-the-job hours; campaigning does take a lot of time away from the desk...

MxB
...ain't that damn truth. [not to mention playing golf too]
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 03-Jul-2012, 10:20
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 656
Mexican Bob is just really niceMexican Bob is just really niceMexican Bob is just really niceMexican Bob is just really nice

Re: Import/Export


Quote:
Originally Posted by TurboPT
...ain't that damn truth. [not to mention playing golf too]

...and, he's got the highest pay rate of the bunch!


MxB
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:15.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.