Thread: Import Export Business

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    15

    Import Export Business

    I'm going to be working on the following project tonight, and while I don't remember seeing the text about doing this, I'm assuming I can take the information out of two files, save them as temp, then if temp1=temp2 import the hours worked. Just wanted to throw that out there before I start coding. Just wanting to make sure I'm on the right path before I dive in.

    Problem:
    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.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    I'm pretty happy with this (I think, I need to check the numbers still) but for some reason endl isn't working and all the information is running together. Any tips on getting the next iteration to go to the next line?

    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
            
        eds >> name >> id1 >> rate;
        while (!eds.eof())
        {
            hrds >> id2 >> hours;
            if (id1 == id2)
            {
                pay = rate * hours;
                pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
                eds >> name >> id1 >> rate;
            }
            else if (id1 != id2)
            {
                hrds >> id2 >> hours;
                if (hrds.eof())
                {
                    pay = 0;
                    pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
                    eds >> name >> id1 >> rate;
                }
            }
        }
    }


    Problem:
    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.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    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.

    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;
    		}
    
    
    	}
    }
    For those interested, the project is as stated below.

    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





    Quote Originally Posted by CoryMore View Post
    I'm pretty happy with this (I think, I need to check the numbers still) but for some reason endl isn't working and all the information is running together. Any tips on getting the next iteration to go to the next line?

    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
            
        eds >> name >> id1 >> rate;
        while (!eds.eof())
        {
            hrds >> id2 >> hours;
            if (id1 == id2)
            {
                pay = rate * hours;
                pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
                eds >> name >> id1 >> rate;
            }
            else if (id1 != id2)
            {
                hrds >> id2 >> hours;
                if (hrds.eof())
                {
                    pay = 0;
                    pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
                    eds >> name >> id1 >> rate;
                }
            }
        }
    }


    Problem:
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First rule: don't crosspost. It makes everyone sad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    My apologies, I didn't know I had cross posted. I did post on a few different forums, are they linked?

    Quote Originally Posted by Elysia View Post
    First rule: don't crosspost. It makes everyone sad.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    I will attempt to resolve the cross posting issue, again I'm sorry. Where I've ended up: the first and last names are the only ones who's pay end up correct, and for some reason they're all on the same line.

    Code:
    //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;
    	
    	
    	eds >> name >> id1 >> rate;
    	hrds >> id2 >> hours;
    	while (!(eds.eof()))
    	{
    		pay = rate * hours;
    			
    	if (id1 == id2)
    	{
    		pds << name << " " << id1 << " " << rate << " " << hours << " $" << pay << endl;
    		eds >> name >> id1 >> rate;
    	}
    	else if (id1 != id2)
    	{
    		hrds >> id2 >> hours;
    		while (id1 != id2)
    		{
    			if (!(hrds.eof()))
    			{
    				hrds >> id2 >> hours;
    			}
    			else
    			{
    				pds << name << " " << id1 << " " << rate << " " << hours << " $" << noHours << endl;
    				eds >> name >> id1 >> rate;
    			}
    		}
    	}
    	
    	}
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CoryMore View Post
    My apologies, I didn't know I had cross posted. I did post on a few different forums, are they linked?
    When posting on several different forums, it is considered crossposting, and a lot of people just don't like it.
    It's better you focus your attention on one forum, and if after a few days, no one can help you, you may try another.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dll Export in C++
    By emilz in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2010, 07:19 AM
  2. Dll Export table
    By P4R4N01D in forum Windows Programming
    Replies: 7
    Last Post: 04-08-2008, 09:41 PM
  3. Can I export this?
    By cboard_member in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2006, 10:53 AM
  4. export vc++ proj to dev c++
    By jay_uccs in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2005, 07:43 AM
  5. How do I export as exe not cpp?
    By flatline911 in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2003, 12:22 AM