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.
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
int main(){
double payRate, total, overTime;
constint 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 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.
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;
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.