The output is not right

Hello,

I am working on a small program for the school. I have done everything, but the output is not as expected to be.
the program is reading data from a text file and using them for some calculation and storing the result in a new text file.

--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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>    //input output stream
#include <fstream>     //file stream
#include <iomanip>     //input output manipulation
using namespace std;

//Function Prototype
bool process_employee (ifstream& inFile);
void process_payroll (ifstream& inFile);
void print_results(ofstream& outFile);

int main()
{

	double gross = 0;
	double bonus = 0;
	double adjGross = 0;

	//Declare file to be read from
	ifstream inFile;
	inFile.open("empInFile.txt");//open the file

	if(!inFile)
	{
		cout << "\n\nThe input file has not opened.\n"
		<< "Check that the file exists.\n" << endl;
		system("pause");
		exit(1);
	}//IF statement to test that the inFile opens

	//Calls to the process_payroll funtion to perform calculations

	process_payroll(inFile);
	system("pause");

	return 0;

}//main

//This function reads the employee information from the input file and returns them through referenced variables
//if the employee information is read successfully it returns 1 otherwise it returns 0.
bool process_employee (ifstream& inFile)
{
	//Declare arrays
	char employee_name [15];
	double hourly_wage [15];
	int hours_worked [15];

	if(inFile >> employee_name, hourly_wage, hours_worked)
		return 1;
	else
		return 0;

}// process_employee function

void process_payroll (ifstream& inFile)
{

	//Local Declarations
	ofstream outFile;

	//Open the outFile
	outFile.open("empReport.txt");//open the file

	if (!outFile)
	{
		cout << "\n\nThe output file has not opened.\n"
			<< endl;
		system("pause");
		exit(1);
	}//IF statement to test that the outFile opens
	else
	{
		cout << "Every thing is working fine." << endl;
	}
	//Calling process_employee function from inside a loop until zero is returned.
	
	while (process_employee(inFile))
	{
		//Calls to the print_summary function to output all calculations
		print_results(outFile);
	}

	//*******************************************************************

	//close the ifstream inFile connection between the program and the file input.txt
	inFile.close();

	return;

}

//This function prints out the Summary message;

void print_results(ofstream& outFile)
{

	//Local Declarations
	int hours = 0;
	int rate = 0;
	int minWage = 0;
	int maxWage = 0;

	double minValue = 100;
	double maxValue = 0;
	double totAvg = 0;

	//Declare arrays
	char employee_name [15];
	double hourly_wage [15];
	int hours_worked [15];
	int empFiles = employee_name[15];

	//Statements
	for(int i = 0; i < empFiles; i++)
	{
		if(hourly_wage[i] > maxValue)
		{
			maxValue = hourly_wage[i];
			maxWage = i;
		}

		if(minValue > hourly_wage[i])
		{
			minValue = hourly_wage[i];
			minWage = i;
		}

		totAvg = totAvg + hourly_wage[i];
	}//For Loop

	//The output of the minimum and maximum wage calculations and the average wage

	outFile << "\n\nThe number of records read: " << empFiles << "\n"
	<< "Maximum Pay Rate: " << employee_name[maxWage] << " @ $" << hourly_wage[maxWage] << "\n"
	<< "Minimum Pay Rate: " << employee_name[minWage] << " @ $" << hourly_wage[minWage] << "\n"
	<< "Average Pay: $" << totAvg / empFiles << endl;

	if(empFiles >= 10)
	{
		outFile << "More than 10 names have been read in."<<endl;//error processing in case more than 10 names are attempted to be read in.
	}

	outFile << "\n\nOctober 2009 Payroll:\t\t\n\n";
	outFile << "Employee Name \t Hours \t Rate \t Gross Bonus \t Adjusted_Gross" << endl;
	outFile << "\n\n\n";

	//format the ouput data
	outFile << std::fixed << setprecision(2) ;

	//Calculate the max and min wages
	//Begin a series of true or false tests with IF Statements and specific expressions for each scenario

	if(hours_worked[hours] > 45)//IF Statement for hours greater than 45
	{

		//Local declaration
		double gross = (hourly_wage[rate] * hours_worked[hours]);
		char bonus = 'Y';
		double adjGross = gross + 50;

		//Statements
		outFile << "\n    "<< employee_name	<< "\t" << hours_worked[hours] << "\t" << hourly_wage[rate] << "\t" << gross << "\t" << bonus << "\t" << adjGross << endl;

	}//IF Statement for hours greater than 45

	if(hours < 30)//IF Statement for hours less than 30
	{
		//Local Declarations and Statements of expressions for calculating each variable
		double gross = (hourly_wage[rate] * hours_worked[hours]);
		char bonus = 'N';
		double adjGross = gross - (hourly_wage[rate] * hours_worked[hours]) - (hours_worked[hours] * .25);

		//Statements
		outFile<< "\n    "<< employee_name << "\t" << hours << "\t"	<< rate << "\t"	<< gross << "\t" << bonus << "\t" << adjGross << endl;

	}//IF Statement for hours less than 30

	if(hours >= 30 && hours <=44)//IF Statement for hours between 35 and 44
	{
		//Local Declarations
		double gross = (hourly_wage[rate] * hours_worked[hours]);
		char bonus = 'N';
		double adjGross = gross;

		//Statements
		outFile<< "\n    "<< employee_name << "\t" << hours << "\t"	<< rate << "\t"	<< gross << "\t" << bonus << "\t" << adjGross << endl;

	}//IF Statement for hours between 35 and 44

	outFile.close();

	return;

}//print_summary
//control passes back to the main function to system("pause") 


--here is what in the input file--
Clinton 10.00 10
Lincoln 5.00 50
Washington 32.00 35
Kennedy 4.99 45
Nixon 10.00 25


--here is what I get in the output file--


The number of records read: -52
Maximum Pay Rate: Ì @ $-9.25596e+61
Minimum Pay Rate: Ì @ $-9.25596e+61
Average Pay: $-0


October 2009 Payroll:		

Employee Name 	 Hours 	 Rate 	 Gross Bonus 	 Adjusted_Gross




    ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ	0	0	79508117989074987650738332503294079560541330881152498119070395440037888.00	N	214748365.00


Last edited on
$ g++ -W{all,extra,pedantic} foo.cpp
|| foo.cpp: In function ‘int main()’:
foo.cpp|14 col 9| warning: unused variable ‘gross’ [-Wunused-variable]
foo.cpp|15 col 9| warning: unused variable ‘bonus’ [-Wunused-variable]
foo.cpp|16 col 9| warning: unused variable ‘adjGross’ [-Wunused-variable]
|| foo.cpp: In function ‘bool process_employee(std::ifstream&)’:
foo.cpp|48 col 43| warning: right operand of comma operator has no effect [-Wunused-value]
|| foo.cpp: In function ‘void print_results(std::ofstream&)’:
foo.cpp|116 col 19| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|118 col 13| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|122 col 30| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|124 col 13| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|128 col 34| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
1
2
//foo.cpp|48 col 43| warning: right operand of comma operator has no effect [-Wunused-value]
	if(inFile >> employee_name, hourly_wage, hours_worked)
to read into a variable use >>
inFile >> a >> b >> c;
however `hourly_wage' is an array, so I'm not sure what you're trying to do there

> warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function
this is a conceptual error
read about local variables and pass by reference
Thank you for your response

I have to read the date from the file to arrays then I have to make the calculation by the arrays and after that, I put the result in the new file.
any help please!!
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
int main(){
	const int n = 15;
	std::string name[n];
	double hourly_wage[n];
	int hours_worked[n];
	double gross[n];

	ifstream input;
	ofstream output;

	int size = 0;
	for(int size=0; size<n; ++size){
		if(not process_employee(input, hourly_wage[size], hours_worked[size]))
			break;
	}


	for(int K=0; K<size; ++K)
		gross[K] = process_payroll(name[K], hourly_wage[K], hours_worked[K]);

	print_results(output, name[K], hourly_wage[K], hours_worked[K], gross[K]);
}

//pass by reference because you want to modify them
bool process_employee(ifstream &input, std::string &name, double &hourly_wage, int &hours_worked){
	if(input>>name>>hourly_wage>>hours_worked)
		return true;
	return false;
}

double process_payroll(int hourly_wage, double hours_worked);
Topic archived. No new replies allowed.