Small payroll problem

Hi everyone I am writing a small program that calculates payroll and writes the results to a text document. However, I am having trouble getting the second and third rate of pay and the second and third total written correctly to the file. It just writes the first rate of pay and first total to all three. I tried putting payrate at another array but no luck. Lines 72,73,94,95 are the troubled lines but the program works right when run just not when writing to the text. Any suggestions would be helpful thank you.
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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){
	double payRate, total, overTime;
	const int num_emp=3;
	int time[num_emp], hours;
	//char ch;
	ofstream payFile;

	/*cout<<"This program will calculate the pay of three workers.\nEnter the number of hours worked for each then the pay rate per hour."<<endl;
	cout<<"Do not enter overtime hours with non-overtime hours."<<endl;
	cin.get(ch);*/

	for(int count=0; count<num_emp; count++){
	cout<<"Enter the number of hours worked by employee:#"<<(count+1)<<endl;
	cin>>time[count];
	hours=time[count];
	cout<<endl;
	}
	
	switch(hours){
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:
	case 9:
	case 10:
	case 11:	
	case 12:	
	case 13:	
	case 14:	
	case 15:	
	case 16:	
	case 17:	
	case 18:	
	case 19:	
	case 20:	
	case 21:	
	case 22:	
	case 23:	
	case 24:	
	case 25:	
	case 26:	
	case 27:	
	case 28:	
	case 29:	
	case 30:	
	case 31:	
	case 32:	
	case 33:	
	case 34:	
	case 35:	
	case 36:	
	case 37:	
	case 38:	
	case 39:	
	case 40:	for(int count=0; count<num_emp;count++){
				payFile.open("payFile.txt");
				cout<<"Employee:#"<<(count+1)<<" enter your hourly pay:";
				cin>>payRate;
				cout<<endl;
				total=(payRate*time[count]);
				cout<<"Employee:#"<<(count+1)<<" worked for "<<time[count]<<" hours and earned $"<<setprecision(5)<<total<<endl<<endl;
				payFile<<"Employee "<<(count+1)<<" you worked for "<<time[0]<<" hour(s) at $"<<payRate<<" per hour and earned $"<<total<<endl;
				payFile<<"Employee "<<(count+2)<<" you worked for "<<time[1]<<" hour(s) at $"<<payRate<<" per hour and earned $"<<total<<endl;
				payFile<<"Employee "<<(count+3)<<" you worked for "<<time[2]<<" hour(s) at $"<<payRate<<" per hour and earned $"<<total<<endl;				
				}
		break;
	case 41:
	case 42:
	case 43:
	case 44:
	case 45:
	case 46:
	case 47:
	case 48:
	case 49:
	case 50:	for(int count=0; count<num_emp;count++){
				payFile.open("payFile.txt");
				cout<<"Employee:#"<<(count+1)<<" enter your hourly pay:";
				cin>>payRate;
				cout<<endl;
				overTime=((time[count]-40)*(payRate/2));
				total=((payRate*time[count])+overTime);
				cout<<"Employee:#"<<(count+1)<<" worked for "<<time[count]<<" hours and earned $"<<setprecision(5)<<total<<endl<<endl;
				payFile<<"Employee "<<(count+1)<<" you worked for "<<time[0]<<" hours at $"<<payRate<<" and earned $"<<setprecision(5)<<total<<endl;
				payFile<<"Employee "<<(count+2)<<" you worked for "<<time[1]<<" hours at $"<<payRate<<" and earned $"<<setprecision(5)<<total<<endl;
				payFile<<"Employee "<<(count+3)<<" you worked for "<<time[2]<<" hours at $"<<payRate<<" and earned $"<<setprecision(5)<<total<<endl;
				}
		break;
	default: cout<<"Invalid number or hours worked! Please try again.\n"<<endl;
		return main();
	}
}
Lines 72-74 are inside your for loop. You're going to write three records to the payfile each time through the loop.

Lines 94-96: same problem.

Line 99: You're making a recursive call to main. You should simply return 1; to indicate failure.

Line 101: A return 0; is missing at the end of the program.

Lines 72-74 and lines 94-96 I am trying to write three records but if I were to enter 10, 20,and 30 for hours worked and then 1, 2, and 3 for rate of pay in the payFile.txt I end up with
Employee 1 you worked for 10 hour(s) at $1 per hour and earned $10
Employee 2 you worked for 20 hour(s) at $1 per hour and earned $10
Employee 3 you worked for 30 hour(s) at $1 per hour and earned $10

The goal would be to get
Employee 1 you worked for 10 hour(s) at $1 per hour and earned $10
Employee 2 you worked for 20 hour(s) at $2 per hour and earned 410
Employee 3 you worked for 30 hour(s) at $3 per hour and earned $90
Any way of helping me get the to rate of pay and total for employee 2 and 3 to be what it should be?

Thank you for the reply too I added the return 1; and return 0;
First time through the loop, you write employee 1, 2, 3 with times t0, t1, t2.
Second time through the loop, your write employee 2, 3, 4 with times t0, t1, t2.
Third time through the loop, you write employee 3, 4, 5 with times t0, t1, t2.

This makes no sense.
Well maybe that is why I am not getting what I am aiming for. Would you be able to give me an example loop that would make sense? It doesn't have to solve my problem for me just an example of something similar so that I would be able to see how to correct it and have it make sense?
You already have the correct information being written to cout.
Just make the write to payfile the same.
i.e. get rid of the second and thrid writes to payfile.
fix the remaing write to payfile to index time.
 
payFile<<"Employee "<<(count+1)<<" you worked for "<<time[count]<<" hours at $"<<payRate<<" and earned $"<<setprecision(5)<<total<<endl;
No one has mentioned the use of the switch.

This is not what switches are for - you would be much better to use if statements for this situation.

Switches are used when you want to select one operation from a list of choices - like in a menu as a simple example.

Your cases 1 to 40 should be tested like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const unsigned NormHours = 40;  //normal hours per week
const unsigned Maxhours = 50;  //maximum hours worked per week

if(hours >= 0 && hours <= NormHours) {
//ordinary pay

}
else if (hours > 40 && hours <= Maxhours) {
//calculate overtime
}
else {
//do error processing
}


HTH
Thank you AbstractionAnon and TheIdeasMan I tried your suggestions and still don't have the correct output to payFile I do appreciate the help and your time spent.
Can You post your new code?
Topic archived. No new replies allowed.