Need help with line sorting on Payroll program

I am trying to get a Payroll program done for school but I cant seem to get the payroll info to align properly. Here is the output.

This payroll program calculates an individual employee pay and company totals
using data from the data file payroll.txt.


A payroll report showing payroll information is displayed.

Emp. Employee       Hourly    Hours     Tax       Gross     Net
No.  Name           Rate      Worked    Rate      Amount    Amount

1    John Smith     10.45     40.00     15.00    418.00    355.30
2
Jane Doe      12.50     45.00     15.00    562.50    478.13
3
Harry Morgan  20.00     40.00     20.00    800.00    640.00
4
Carmen Martinez25.00     35.00     25.00    875.00    656.25
5
Jacintha Washington50.85     60.00     35.00   3051.00   1983.15


Totals:                                              5706.50   4112.83
Press any key to continue . . .


I don't know why the first line is the only line to correctly display the way it should. But anyway here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//header files
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;
//function prototypes
void instructions();
void reportTitle();
void displayEmployeeInfo(string& employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount);
void totalAmounts(double totalGrossAmount, double totalNetAmount);
void readData(istream& inFile, string& employeeName, double& hourlyRate, double& hoursWorked, double& taxRate);
void calculate(double hourlyRate, double hoursWorked, double taxRate, double& grossAmount, double& netAmount, double& totalGrossAmount, double& totalNetAmount);

int main()
{
	//declare input file variable
	ifstream inFile;

	// declare input file variables
	string employeeName;
	double hourlyRate, hoursWorked, taxRate, grossAmount = 0, netAmount = 0, totalGrossAmount = 0, totalNetAmount = 0;

	// open the input file.
	inFile.open("payroll.txt");

	/*check for input failure in case the drive is not accessible. If true, display a message that the input file is not accessible, return the integer 1 from the main program.*/
	if (!inFile)
	{
		cout << "input failed." << endl;
		return 1;
	}

	//call functions for program instructions, formatting, and info display in columns
	instructions();
	reportTitle();

	cout << endl;

	while (!inFile.eof())
	{

	readData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);

    calculate(hourlyRate, hoursWorked, taxRate, grossAmount, netAmount, totalGrossAmount, totalNetAmount);

	displayEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate, grossAmount, netAmount);

	}
	inFile.close();

	cout << endl;
	cout << endl;

	totalAmounts(totalGrossAmount, totalNetAmount);

	cout << endl;

	return 0;
}
//displays program instructions
void instructions()
{
	cout << "This payroll program calculates an individual employee pay and company totals\nusing data from the data file payroll.txt.\n\n\nA payroll report showing payroll information is displayed.\n" << endl;
}
/*function reportTitle displays the payroll report titles in columnar format*/
void reportTitle()
{
	//set program formatting
	cout << setprecision(2) << fixed << showpoint << left;
	//display report titles
	cout << setw(5) << "Emp." << setw(15) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours" << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
	cout << setw(5) << "No. " << setw(15) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}
/*function displayEmployeeInfo displays employee info in while loop line by line until end of file is reached*/
void displayEmployeeInfo(string& employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount)
{
	static int counter = 1;

	cout << left << setw(5) << counter << setw(15) << employeeName << right << setw(5) << hourlyRate << setw(10) << hoursWorked << setw(10) << taxRate << setw(10) << grossAmount << setw(10) << netAmount << endl;

	counter = counter +1;
}
/*function totalAmounts displays total gross and net amount*/
void totalAmounts(double totalGrossAmount, double totalNetAmount)
{
	cout << left << setw(20) << "Totals:" << right << setw(40) << totalGrossAmount  << setw(10) << totalNetAmount;
}
// function readData reads input data from payroll.txt
void readData(istream& inFile, string& employeeName, double& hourlyRate, double& hoursWorked, double& taxRate)
{   
	getline(inFile,employeeName,'#');
	inFile >> hourlyRate >> hoursWorked >> taxRate;
}
// function calculate calculates gross amount and net amount and total gross and net amount from all employees.
void calculate(double hourlyRate, double hoursWorked, double taxRate, double& grossAmount, double& netAmount, double& totalGrossAmount, double& totalNetAmount)
{
	grossAmount = hourlyRate*hoursWorked;
	netAmount = grossAmount - grossAmount * (taxRate/100);
	totalGrossAmount = totalGrossAmount + grossAmount;
	totalNetAmount = totalNetAmount + netAmount;
}
Hi,

I wonder if we're in the same class, lol. I'm just taking a regular C++ at a community college.

If possible, given that we're at roughly the same level (semester nearly over) you might want to consider avoiding the array display, and just list the results as columns:

[code]
emp#
name
hours
rate
etc...
[\code]

I suggest avoiding it because unless you want to sculpt the array based on known inputs (i.e., you know all the data to be entered), making it display with even spacing and good alignment will be tricky. In my class, we generally don't know what the prof. will enter into the .exe, so aesthetics aren't a big deal.

Something you might be aware of (and likely more important than how results print out) is that your [code]void calculate[\code] function doesn't consider overtime for work weeks in excess of 40 hours... but your prof. might not have asked for it, so I don't know.

My $0.02
Hi. Well you know I am not really doing arrays with this problem, and I am aware of the program not considering overtime for work weeks, but I will fix that later. Right not though I just want to get my results to display right, but just how can I tweak the void displayEmployeeInfo function in order t do this?

I still dont understand how my entire output could be formatted to keep this:

2
Jane Doe 12.50 45.00 15.00 562.50 478.13


from happening.

Just why is it that the employee number is always showing on a line that is above the rest of the employee info?
Topic archived. No new replies allowed.