Problem with Output Alignment and Function

This program is a Payroll Report, here's a link to the Data File,

http://www.cerritos.edu/jwilson/cis_180/SHP_Assignments/paydata.txt

Basically my problem is with the output alignment and the function that rounds the calculations. The numbers are never being rounded. Here's a link to the assignment, http://www.cerritos.edu/jwilson/cis_180/SHP_Assignments/CIS-180-SHP-Assignment-1.htm

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;


double grossPayRound(double);

	char empNum[12];
	char empFName[12];
	char empLName[18];
	double empRegHours;
	double empOTHours;
	double empPayRate;
	double grossPay = 0;
	double netPay = 0;
	double grossPayTotal = 0;
	double fedTaxTotal = 0;
	double fedTax = 0;
	double stateTaxTotal = 0;
	double stateTax = 0;
	double ficaTaxTotal = 0;
	double ficaTax = 0;
	double netPayTotal = 0;
	double extraPay = 0;
	double OTPay = 0;

int main()
{

	//Variables
	const double FED_TAX = 0.18;
	const double STATE_TAX = 0.05;
	const double FICA_TAX = 0.0751;
	
	//Instances of inFile/outFile
	ifstream inFile; 
	ofstream outFile;
	inFile.open("PayRoll.txt");
	outFile.open("PayRpt.txt");
	
	
	//Output Headers
	outFile << "\n\t\t\t\t             Wexler U. Payroll Register\n\n"
			  << " Soc. Sec.\t   Name\t\t     Gross Pay\t      Federal Tax\tState Tax\t FICA\t    Net Pay\n";
	
	
	if(!inFile)
	{
		cout << "Error. Could Not Open Data File!" << endl;
		exit(1);
	}
	
	while (!inFile.eof() )
	{
	inFile >> empNum;
	inFile >> empFName;
	inFile >> empLName;
	inFile >> empRegHours;
	inFile >> empOTHours;
	inFile >> empPayRate;

	//Calculations
	grossPay = empPayRate * empRegHours;
	
	//Decision for OverTime Hours
	if (empOTHours > 0)
	{
		grossPay = empPayRate * empRegHours;
		extraPay = (empPayRate * 1.5) * empOTHours;
		OTPay = extraPay + grossPay;
		grossPay = OTPay;
	}
	
	grossPayRound(grossPay);
	
	fedTax = grossPay * FED_TAX;
	grossPayRound(fedTax);
	
	stateTax = grossPay * STATE_TAX;
	grossPayRound(stateTax);
	
	ficaTax = grossPay * FICA_TAX;
	grossPayRound(ficaTax);
	
	
	//Output
	outFile << empNum << "\t" << empFName << setw(8) << empLName << "\t\t" << setw(7) << grossPay << "\t\t" << fixed << setprecision(2) << setw(5) << fedTax 
		     << "\t\t " << stateTax << "\t\t" << ficaTax << "  " << "\n";
	
	//Accumulators
	grossPayTotal += grossPay;
	fedTaxTotal += fedTax;
	stateTaxTotal += stateTax;
	ficaTaxTotal += ficaTax;
	
	}

	//Front Intro Page
	cout << "\n\n\t\t    ********************************\n"
	 	  << "\n\t\t\t  CIS 180 Honors Work\n\n\t\t\t  Pay Roll Data Report\n"
	     << "\n\t\t\t\t   By \n\n\t\t\t   Trevor Davenport"
	     << "\n\n\t\t    ********************************\n\n\n"
		  << "DESCRIPTION: This Program Reads Input From A PayRoll Data File,\n"
		  << "\t     Calculates Gross Pay Rounded to TWO Decimals,\n"
		  << "\t     Deducts Federal, State, and FICA Taxes,\n"
		  << "\t     And Returns The Above Mentioned In A Output Report.\n\n";
		  
	//Grand Totals Summary Line	  
	outFile << "\n\t\tGrand Totals:   \t" << grossPayTotal << "\t\t" << fedTaxTotal << "\t\t" << stateTaxTotal << "\t\t" << ficaTaxTotal; 	  

	system("pause");
	system("cls");


			 	
	inFile.close();
	outFile.close();
	
}


double grossPayRound(double grossPay)
{
	double answer;
	int x;
	answer = (grossPay + .0051) * 100;
	x = int (answer/100);
	return x;

}

				             Wexler U. Payroll Register

 Soc. Sec.	   Name		     Gross Pay	      Federal Tax	State Tax	 FICA	    Net Pay
384-92-7122	MUTH,  WARREN		    420		75.60		 21.00		31.54  
271-83-9400	JACKSON,  ELMIRA		 310.62		55.91		 15.53		23.33  
261-83-9448	CRANDALL,   ROGER		 673.75		121.27		 33.69		50.60  
382-71-9220	ROUSSEAU,  DARRYL		 457.50		82.35		 22.88		34.36  
385-71-1824	MURRAY,    RITA		 162.50		29.25		 8.12		12.20  
271-83-9402	FREIWALD,    EMMA		 270.83		48.75		 13.54		20.34  
382-28-9277	GREENBERG,WINIFRED		 620.00		111.60		 31.00		46.56  
462-71-8096	MOCHIZUKI,     MAX		 694.86		125.08		 34.74		52.18  
382-71-1654	LEMMER,ELIZABETH		 357.70		64.39		 17.89		26.86  
384-92-4855	MCGINNIS,CLIFFORD		1032.50		185.85		 51.62		77.54  
263-84-7760	KRUIZENGA,  GLADYS		 105.00		18.90		 5.25		7.89  
277-65-9813	BROWN,     JAY		 358.01		64.44		 17.90		26.89  
387-53-7283	DIETERMAN, CHARLES		 586.38		105.55		 29.32		44.04  
274-82-3910	YZENBAARD,     JAN		 206.85		37.23		 10.34		15.53  
384-49-2189	VANDUSEN, WILLIAM		 737.20		132.70		 36.86		55.36  
273-61-9920	RAMIREZ,   PABLO		 479.71		86.35		 23.99		36.03  
381-72-2699	LEE,  WALTER		 743.90		133.90		 37.20		55.87  

		Grand Totals:   	8217.33		1479.12		410.87		617.12
anyone?
You first convert to int, then you divide by 100. Right now your function divides by 100 BEFORE converting to int. But then again you also have a big problem using the function. You are discarding the return value. You should call it like this:

1
2
fedTax = grossPay * FED_TAX;
fedTax = grossPayRound(fedTax);
Thanks for the reply man, So If I'm following you correctly..

1
2
3
4
5
6
7
8
9
double grossPayRound(double grossPay)
{
	double answer;
	int x;
	answer = (grossPay + .0051) * 100;
	x = int (answer) / 100
	return x;

}


However, it still doesn't round them. I also changed the function calls as you suggested.
You are returning x, which is an int, but you need to return 'answer'. In the previous-to-last line, instead of using x in the left side, also use 'answer', and force a double computation. See my test function:

1
2
3
4
5
6
7
template<class T>
T RoundNumber(const T &input)
{
	T retVal = (input + 0.0051) * 100;
	retVal = T(int(retVal)) / 100;
	return retVal;
}


If you don't know about templates, then delete line 1 and change all T's for double. If you know about templates, you should note that this function will serve you well for doubles and floats.
Sorry I'm not familiar with templates.. I'm not too sure I understand what you mean. I understand that I need to return answer.. but as using answer on left side? So I don't even need an integer X variable is what your saying?


1
2
3
4
5
6
7
8
9
double grossPayRound(double grossPay)
{
	double answer;
	int x;
	answer = (grossPay + .0051) * 100;
	answer = int (answer) / 100
	return answer;

}
Almost:

1
2
3
4
5
6
double RoundNumber(const double input)
{
	double retVal = (input + 0.0051) * 100;
	retVal = double(int(retVal)) / 100;
	return retVal;
}


And yes, the variable x is not needed at all.
So I changed the function to how you explained.. However, it still doesn't seem to be working properly.

1
2
3
4
5
6
7
8
double grossPayRound(double grossPay)
{
	double answer;
	answer = (grossPay + .0051) * 100;
	answer = double(int(answer)) / 100;
	return answer;

}



Output still hasn't been rounded..
Topic archived. No new replies allowed.