Write a c++ program that will read in an unknown number of employee records from a text file call “PAYDATA3.TXT”. Each employees record is a string of 47 characters (with spaces), set up as follows:

Positions Data Description
0 – 14 Last Name (15 characters)
15 -24 First name (10 characters)
25 Contains C if employee works in City office, otherwise S for Suburb
26 Contains M if employee is a union member, X if not a union member
27 – 31 Employee Number (5 digit characters)
32 – 35 Hours Worked (4 characters, in tenths)
36 – 40 Hourly Rate of Pay (5 characters)
41-42 Number of Dependents (2 characters)
43 – 46 Overtime hours worked (4 characters, in tenths, if any)

The program must perform the following tasks:
A. For each employee, calculate the gross pay
Gross = hours worked times hourly rate plus overtime hours time hourly rate times 1 ½
B. For each employee calculate the net pay as follows:
a. net pay = gross minus deductions
b. deductions are calculated as follows:
Federal Tax = (gross minus 17 times no. of dependents) times 18.34%
FICA = gross times 7.65%
City Tax = $0.00 if employee works in the suburbs
4% of gross if employee works in the City
Union Dues = $0.00 if employee is not a union member
4.73% of gross if employee is a union member

For each employee, print 1 line of information as follows: (In WORDPAD, File>
Page Setup>Landcape) [Screen will show 2 lines for each employee – This is OK]
Emp. # First Name Last Name Hours Rate Overtime Gross Fed Tax FICA City Tax Union Dues Net Pay
(All with 2 decimals)

Also compute and print (decimal points must line up vertically)
1. Number of employees processed = XX
2. Total Gross Pay = XXXX.XX
3. Total Federal Tax = XXXX.XX
4. Total FICA = XXXX.XX
5. Total Hours Worked = XXX.XX
6. Total Overtime Hours Worked = XXX.XX

Below, # is a space

Example: Jefferson######Alford####CM2111239.811.340406.6

21112 Alford Jefferson 39.80 11.34 6.60 563.60 90.89 43.12 22.54 26.66 380.39

This is what I have:

#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<sstream>
#include<iostream>

using namespace std;
int main()
{

	ifstream infile;
    ofstream outfile;

    infile.open ("paydata3.txt");
    outfile.open ("payOutput");

	string input, tmp;
	double totalemp=0;
	double totalgross=0;
	double totalfed=0;
	double totalfica=0;
	double totalhours=0;
	double totalover=0;
		
	outfile<<"Number of employees processed ="<<setw(10)<<totalemp<<endl;
	outfile<<"Total Gross Pay ="<<setw(10)<<totalgross<<endl;
	outfile<<"Total Federal Tax ="<<setw(10)<<totalfed<<endl;
	outfile<<"Total FICA ="<<setw(10)<<totalfica<<endl;
	outfile<<"Total Hours Worked ="<<setw(10)<<totalhours<<endl;
	outfile<<"Total Overtime Hours Worked ="<<setw(10)<<totalover<<endl;
	outfile<<"Emp. #"<<setw(14)<<"First Name"<<setw(12)<<"Last Name"<<setw(6)<<"Hours"<<setw(8)<<"Rate"<<setw(12)<<"Overtime"<<setw(8)<<"Gross"<<setw(12)<<"Fed Tax"<<setw(10)<<"FICA"<<setw(12)<<"City Tax"<<setw(14)<<"Union Dues"<<setw(16)<<"Net Pay"<<endl;
	while (getline(infile,input))
	{  
		
    
    //  declare variables to hold the values from the inputted line; not all have been used.
    string fname, lname, emplnum;
    char cityOrSuburb, unionOrNot;
    float hoursWorked, payRate, overtime, gross, fedtax, fica, ctax, netpay, undues, deductions;
    int dependents;

	{
    
    
    stringstream inputstream;
    
    //  creat temporary string from the substring
    tmp=input.substr(0,14);
    
    //  assign string to the stringstream
    inputstream.str(tmp);
    
    //  use extraction operator to get formatted value from stringstream
    inputstream >> lname;
	totalemp=totalemp++;
    
    
    //  repeat for a few more of the entries
    tmp=input.substr(15,10);
    inputstream.str(tmp);
    inputstream >> fname;
	inputstream.clear();
    
    tmp=input.substr(25,1);
    inputstream.str(tmp);
    inputstream >> cityOrSuburb;
	inputstream.clear();
    
    tmp=input.substr(26,1);
    inputstream.str(tmp);
    inputstream >> unionOrNot;
	inputstream.clear();

	tmp=input.substr(27,5);
	inputstream.str(tmp);
	inputstream >> emplnum;
	inputstream.clear();


	tmp=input.substr(32,4);
	inputstream.str(tmp);
	inputstream >> hoursWorked;
	inputstream.clear();
	totalhours=totalhours++;

	
	tmp=input.substr(36,5);
	inputstream.str(tmp);
	inputstream >> payRate;
	inputstream.clear();

	
	tmp=input.substr(41,2);
	inputstream.str(tmp);
	inputstream >> overtime;
	inputstream.clear();
	totalover=totalover++;


	tmp=input.substr(43,4);
	inputstream.str(tmp);
	inputstream >> dependents;
	inputstream.clear();

	{
		gross = (hoursWorked * payRate);
		totalgross=totalgross++;
	}
{
	fedtax = ((gross - (dependents*17.0))*.1834);
	totalfed=totalfed++;
{
	fica = (gross * .0765);
	totalfica=totalfica++;
}

if (cityOrSuburb == 'S')
{
ctax = 0.00;
}
else if (cityOrSuburb == 'C')
{
ctax = (gross * 0.04);
}

if (unionOrNot == 'X')
{ 
undues = 0.00;
} 
else if ( unionOrNot == 'M')
{
undues = (gross * .0473);
}

deductions = (fedtax + fica + ctax + undues); 
netpay = (gross - deductions);

}

}
outfile<<emplnum<<setw(14)<<fname<<setw(12)<<lname<<setw(6)<<hoursWorked<<setw(8)<<payRate<<setw(12)<<overtime<<setw(8)<<gross<<setw(12)<<fedtax<<setw(10)<<fica<<setw(12)<<ctax<<setw(14)<<undues<<setw(16)<<netpay<<endl;
}

    infile.close();
    outfile.close();
    return 0;
}

How can i get the following:
Also compute and print (decimal points must line up vertically)
Number of employees processed
Total Gross Pay
Total Federal Tax
Total FICA
Total Hours Worked
Total Overtime Hours Worked
It compiles ok with the following infile:

Schanz         Hannelore SX1234540.009.551000.0
Liffer         Umfritz   CM2345638.508.670301.5
Ajahb          Fyndmee   CX3456739.606.830203.9
Zyzz           Abba      CX4567840.310.910200.5
Goodgrayed     Ineeda    SM5678938.713.520202.0
Bath           Uneeda    CM6789039.311.110102.5
Strongbreeze   Ifelta    CM7890139.607.930300.0
Anapple        Iyate     SX8901239.905.950000.0
Kulate         Cal       SX9012340.007.870104.0
Isemtee        Mytank    CM0123440.514.880712.5
Lihd           Flippa    CM1123440.112.220410.3
Lyfe           Getta     CM2234540.206.750309.4
Franzipanni    Gwoppo    SM3345637.805.920205.3
Sanchez        Fillipina CX4456738.311.220106.1
Regulurr       Ear       SX5567839.410.050300.0
Enstance       Hap       SM6678940.409.250204.4
Lawreeyat      Backa     CM7789040.009.310202.2
Doothis        Ugonna    CM8890139.013.250203.3
Rupowch        Kanga     CM9901238.615.960612.8
Lewser         Sheesa    SX1112344.312.940308.1
Bustid         Mikars    SM2223440.308.880502.3
Mylegg         Letgo     CM3334548.913.380811.1
Stee           Iyamthur  CX4445644.408.880101.7

Thanks for your input and also please take a look at my other assignment.

Recommended Answers

All 15 Replies

>>How can i get the following:
Looks simple enough -- just add more variables to hold the those totals and add them up while reading the file.

I thought i did by the use of:

totalfica=totalfica++;
totalfed=totalfed++;
totalgross=totalgross++;
totalemp=totalemp++;

or would it be:

totalemp++;(instead of counter++)
totalemp+=

....and that would go inside the while loop?

> totalfica=totalfica++;
Try just
totalfica++;

as what you tried is undefined, and quite likely to not produce the result you were expecting.

you are supposed to be summing up the total amounts, not counting how many there are. For example Total Gross Pay is:

grossPay = ???
totalgross += grossPay;

>>and that would go inside the while loop?
Yes because those calculations have to be done for each line in the file

Thx, Salem, that's what i had originally thought, but doesn't that increments the value by 1?

grossPay = ???
totalgross += grossPay;

Well, i was using totalgross as the gross for all of the employees, but you used grossPay. So i guess you were trying to say:

grossPay=;
totalgross += grossPay;

It doesn't work, do you thinks it could also be because of variables outside of my while loop?

The total variables have to be declared outside the loop so that they don't get re-initialized on every loop iteration.

>>It doesn't work
Then you didn't do it right. In the example I posted, grossPay is the gross pay for the current line in the file. Gross pay is not included in the file -- you have to calculate it based on other information in the file

Gross = hours worked times hourly rate plus overtime hours time hourly rate times 1 ½

Thanks for spotting the calcualtions part:

gross = (hoursWorked * payRate) + (1.5*payRate*overtime);
		totalgross += gross;

That doesn't work either...and i guess that after iget this part sorted out, that it will be the format for the other totals that i need. Thx for the assistance.

>>That doesn't work either
why do you say it doesn't work??? Looks ok to me.

It still shows the total as being '0', in the totals section. I know the infile is ok, because everything except for the totals. Let me re-post my code. Thx for the assistance.

#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<sstream>
#include<iostream>

using namespace std;
int main()
{

	ifstream infile;
    ofstream outfile;

    infile.open ("paydata3.txt");
    outfile.open ("payOutput");

	string input, tmp;
	double totalemp=0;
	double totalgross=0;
	double totalfed=0;
	double totalfica=0;
	double totalhours=0;
	double totalover=0;
		
	outfile<<"Number of employees processed ="<<setw(10)<<totalemp<<endl;
	outfile<<"Total Gross Pay ="<<setw(10)<<totalgross<<endl;
	outfile<<"Total Federal Tax ="<<setw(10)<<totalfed<<endl;
	outfile<<"Total FICA ="<<setw(10)<<totalfica<<endl;
	outfile<<"Total Hours Worked ="<<setw(10)<<totalhours<<endl;
	outfile<<"Total Overtime Hours Worked ="<<setw(10)<<totalover<<endl;
	outfile<<"Emp. #"<<setw(14)<<"First Name"<<setw(12)<<"Last Name"<<setw(6)<<"Hours"<<setw(8)<<"Rate"<<setw(12)<<"Overtime"<<setw(8)<<"Gross"<<setw(12)<<"Fed Tax"<<setw(10)<<"FICA"<<setw(12)<<"City Tax"<<setw(14)<<"Union Dues"<<setw(16)<<"Net Pay"<<endl;
	while (getline(infile,input))
	{  
		
    
    //  declare variables to hold the values from the inputted line; not all have been used.
    string fname, lname, emplnum;
    char cityOrSuburb, unionOrNot;
    float hoursWorked, payRate, overtime, gross, fedtax, fica, ctax, netpay, undues, deductions;
    int dependents;

	{
    
    
    stringstream inputstream;
    
    //  creat temporary string from the substring
    tmp=input.substr(0,14);
    
    //  assign string to the stringstream
    inputstream.str(tmp);
    
    //  use extraction operator to get formatted value from stringstream
    inputstream >> lname;
	    
    //  repeat for a few more of the entries
    tmp=input.substr(15,10);
    inputstream.str(tmp);
    inputstream >> fname;
	inputstream.clear();
    
    tmp=input.substr(25,1);
    inputstream.str(tmp);
    inputstream >> cityOrSuburb;
	inputstream.clear();
    
    tmp=input.substr(26,1);
    inputstream.str(tmp);
    inputstream >> unionOrNot;
	inputstream.clear();

	tmp=input.substr(27,5);
	inputstream.str(tmp);
	inputstream >> emplnum;
	inputstream.clear();

	tmp=input.substr(32,4);
	inputstream.str(tmp);
	inputstream >> hoursWorked;
	inputstream.clear();
		
	tmp=input.substr(36,5);
	inputstream.str(tmp);
	inputstream >> payRate;
	inputstream.clear();
	
	tmp=input.substr(41,2);
	inputstream.str(tmp);
	inputstream >> overtime;
	inputstream.clear();
	
	tmp=input.substr(43,4);
	inputstream.str(tmp);
	inputstream >> dependents;
	inputstream.clear();

	{
		gross = (hoursWorked * payRate) + (1.5*payRate*overtime);
		totalgross += gross;
	}
{
	fedtax = ((gross - (dependents*17.0))*.1834);
	
{
	fica = (gross * .0765);
	
}

if (cityOrSuburb == 'S')
{
ctax = 0.00;
}
else if (cityOrSuburb == 'C')
{
ctax = (gross * 0.04);
}

if (unionOrNot == 'X')
{ 
undues = 0.00;
} 
else if ( unionOrNot == 'M')
{
undues = (gross * .0473);
}

deductions = (fedtax + fica + ctax + undues); 
netpay = (gross - deductions);

}

}
outfile<<emplnum<<setw(14)<<fname<<setw(12)<<lname<<setw(6)<<hoursWorked<<setw(8)<<payRate<<setw(12)<<overtime<<setw(8)<<gross<<setw(12)<<fedtax<<setw(10)<<fica<<setw(12)<<ctax<<setw(14)<<undues<<setw(16)<<netpay<<endl;
}

    infile.close();
    outfile.close();
    return 0;
}

The total output shows zero. Any help is appreciated.

for starters, move lines 27-33 down after everything has been calculated. You can't print the values of the variables before the calculations have been completed.

Thx for my reply, however, i've moved those headers out and it still won't work, still says the values are zero. Let me repost my new code. Thx again AD!!

#include <iomanip>
#include <cmath>
#include <fstream>
#include <vector>
#include<string>
#include<sstream>
#include<iostream>

using namespace std;
int main()
{

	ifstream infile;
    ofstream outfile;

    infile.open ("paydata3.txt");
    outfile.open ("payOutput");

	string input, tmp;
	double totalemp=0;
	double totalgross=0;
	double totalfed=0;
	double totalfica=0;
	double totalhours=0;
	double totalover=0;
	double grossPay = 0;
		
	outfile<<"Emp. #"<<setw(14)<<"First Name"<<setw(12)<<"Last Name"<<setw(6)<<"Hours"<<setw(8)<<"Rate"<<setw(12)<<"Overtime"<<setw(8)<<"Gross"<<setw(12)<<"Fed Tax"<<setw(10)<<"FICA"<<setw(12)<<"City Tax"<<setw(14)<<"Union Dues"<<setw(16)<<"Net Pay"<<endl;
	while (getline(infile,input))
	{  
		
    
    //  declare variables to hold the values from the inputted line; not all have been used.
    string fname, lname, emplnum;
    char cityOrSuburb, unionOrNot;
    float hoursWorked, payRate, overtime, gross, fedtax, fica, ctax, netpay, undues, deductions;
    int dependents;

	{
    
    
    stringstream inputstream;
    
    //  creat temporary string from the substring
    tmp=input.substr(0,14);
    
    //  assign string to the stringstream
    inputstream.str(tmp);
    
    //  use extraction operator to get formatted value from stringstream
    inputstream >> lname;
	    
    //  repeat for a few more of the entries
    tmp=input.substr(15,10);
    inputstream.str(tmp);
    inputstream >> fname;
	inputstream.clear();
    
    tmp=input.substr(25,1);
    inputstream.str(tmp);
    inputstream >> cityOrSuburb;
	inputstream.clear();
    
    tmp=input.substr(26,1);
    inputstream.str(tmp);
    inputstream >> unionOrNot;
	inputstream.clear();

	tmp=input.substr(27,5);
	inputstream.str(tmp);
	inputstream >> emplnum;
	inputstream.clear();

	tmp=input.substr(32,4);
	inputstream.str(tmp);
	inputstream >> hoursWorked;
	inputstream.clear();
	
		
	tmp=input.substr(36,5);
	inputstream.str(tmp);
	inputstream >> payRate;
	inputstream.clear();
	
	tmp=input.substr(41,2);
	inputstream.str(tmp);
	inputstream >> overtime;
	inputstream.clear();
	
	
	tmp=input.substr(43,4);
	inputstream.str(tmp);
	inputstream >> dependents;
	inputstream.clear();

	{
		gross = (hoursWorked * payRate) + (1.5*payRate*overtime);
		gross += totalgross;
	}
{
	fedtax = ((gross - (dependents*17.0))*.1834);
	{
	fica = (gross * .0765);
	
}

if (cityOrSuburb == 'S')
{
ctax = 0.00;
}
else if (cityOrSuburb == 'C')
{
ctax = (gross * 0.04);
}

if (unionOrNot == 'X')
{ 
undues = 0.00;
} 
else if ( unionOrNot == 'M')
{
undues = (gross * .0473);
}

deductions = (fedtax + fica + ctax + undues); 
netpay = (gross - deductions);


}

}
outfile<<emplnum<<setw(14)<<fname<<setw(12)<<lname<<setw(6)<<hoursWorked<<setw(8)<<payRate<<setw(12)<<overtime<<setw(8)<<gross<<setw(12)<<fedtax<<setw(10)<<fica<<setw(12)<<ctax<<setw(14)<<undues<<setw(16)<<netpay<<endl;

}
	outfile<<"Number of employees processed ="<<setw(10)<<totalemp<<endl;
	outfile<<"Total Gross Pay ="<<setw(10)<<totalgross<<endl;
	outfile<<"Total Federal Tax ="<<setw(10)<<totalfed<<endl;
	outfile<<"Total FICA ="<<setw(10)<<totalfica<<endl;
	outfile<<"Total Hours Worked ="<<setw(10)<<totalhours<<endl;
	outfile<<"Total Overtime Hours Worked ="<<setw(10)<<totalover<<endl;


    infile.close();
    outfile.close();
    return 0;
}

My reason for placing the headers where they are, is because if i place them any where else they are going to print more than once. I still get zero for my answers.

Thanks to all for the help. Problem solved.

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.