Input Files

I am having trouble writing this code. I am somewhat close I think but I really need some help. Here is the prompt...


Problem Statement
Assume you are reading lines of data that correspond to records of employee's data. For example:
x135.5 14.56 999999999 Kenneth Todd Stevens
On each line, the first character is not used (so read it into a junk variable). The second character indicates which type of employee the individual is:
1. part-time hourly,
2. part-time salary,
3. full-time hourly without overtime,
4. full-time-hourly with double pay overtime
5. full-time salary
Use a if statements (or switch) on this character, and have a case for each type in which you will call the respective calc function to return the employee's weekly pay.

Immediately after each type character (1-5) is how many hours (float) the employee worked that week, then white-space, and then either their annual salary (double) or their hourly rate (double). Each field after the hours-worked is white-space separated. Next are their employee id number (long), first name, middle name, last name.

Write the following separate functions to calculate each employee's pay for the week:
• calcPartTimeHourly(float hours, double hrate)
- never paid for more than 39 hours
• calcPartTimeSalary(float hours, double sal)
• calcFullTimeHourlyWithoutOvertime(float hours, double hrate)
- never paid for over 40 hours
• calcFullTimeHourlyWithOvertime(float hours, double hrate)
- paid double-time for over 40 hours
• calcFullTimeSalary(float hours, double sal)
Each of these must return the calculated pay as a double. The functions must be named exactly this and have exactly these parameter types.


Sample Input
Name of input file: infile.txt

Sample Input File
a222.5 10000.00 329238390 Matthew Charles Mullenweg
g3999 59.99 327237237 Sergey Mikhailovich Brin
x1100.1 200 100000000 Edsger Wybe Dijkstra
p51 9999.99 123456890 Guido van Rossum
c499.9 100.01 777777777 Kenneth Lane Thompson

Sample Output (corresponding to the above input)
The id is 9 characters printed in a 12 character field; the name field is 15 characters, and the next 2 columns are first initial and middle initial. The other fields have just 1 space between them.
Name of input file: empInput.txt
329238390 Mullenweg M C $192.31
327237237 Brin S M $2399.60
100000000 Dijkstra E W $7800.00
123456890 Rossum G v $192.31
777777777 Thompson K L $15981.60
Number of Employees: 5
Number of Hours Worked: 1222.50
Total Payroll: $26565.81



Hint: You can access just the first character of a string with fname[0];


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
  int main()
{
	ifstream infile;
	ofstream  myOutfile;


	char junk;
	float hours;
	float weekpay;
	double annual_salary;
	double hrate;
	double sal;
	long employeeID;
	long firstname, middlename, lastname;
	double calcPartTimeHourly;
	double calcPartTimeSalary;
	double calcFullTimeHourlyWithoutOvertime;
	double calcFullTimeHourlyWithOvertime;
	double calcFullTimeSalary;
	char type;
	string inputfile;



	cout << "Name of input file: " << endl;
	
	infile.open(inputfile);
	myOutfile.open(inputfile);
	

	




	if (! infile) // the ! means myInfile is non 0, indicating it is OK

	{
		cout << "Error" << endl;
		return 1;

	}
	


	infile >> junk >> hours >> annual_salary >> employeeID >> firstname >> middlename >> lastname;


	cin >> type;


	if (type==1)

	{
		cin >> hours;
		cin >> hrate;
		calcPartTimeHourly = hours * hrate;

	}

	else if (type==2)

	{
		cin >> hours;
		cin >> sal;
		calcPartTimeSalary = hours * sal;
	}

	else if (type==3)

	{
		cin >> hours;
		cin >> hrate;

		calcFullTimeHourlyWithoutOvertime = hours * hrate;
	}

	else if (type==4)

	{
		cin >> hours;
		cin >> hrate;
		calcFullTimeHourlyWithOvertime = ((hours * hrate) + (40 - hours) * (hrate * 1.5));
	}

	else if (type==5)

	{
		cin >> hours;
		cin >> sal;
		calcFullTimeSalary = (hours * sal);
	}

	


	return 0;
}

bump
Can you be more specific about where you're getting stuck? I think that will make it easier for someone to help out.
Let's start with this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	if (! infile) // the ! means myInfile is non 0, indicating it is OK

	{
		cout << "Error" << endl;
		return 1;

	}
	


	infile >> junk >> hours >> annual_salary >> employeeID >> firstname >> middlename >> lastname;


	cin >> type;

The comment on the first line above might be misleading. That line is testing to insure that the stream is not in an error state. And remember that a Boolean expression yields one of two values when converted to an integral, zero (false) or not zero (true).

Next to the infile >> line. Do realize that this is reading from your input file? But it doesn't appear to be correct to me. Remember you have two characters before the hours field, a "junk value", then the "type of employee", then the hours.

Next why are you asking the user for the type, remember cin is Console INput? Everything should be coming from your file. You should be reading the first two characters before your if() statements, then depending on the "type" the data is read from the file into the correct variables for that type, then you call the correct function to do the calculations.

What I need to do is open the file, and have an output file based on the input file. But you are right about that being read wrong jilb. I believe it should look like this. I just don't know how to get it to the output format. And I believe you are right, I don't need to ask the user for the hours since it is given in the input file.
I just don't know how to get it to the output format.

Until you can properly read the file there is no need to worry about the output format. The first thing you need to do is properly read the file. So start simple, read the first two characters, of each line, determine the "type" of employee and print a message informing the user of the type of employee on each line.



Jim
Topic archived. No new replies allowed.