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();
}
}
The reason for not writing the three employees details in the file is because the payFile.rtf is opened everytime the for loop executes. This will make the previous contents in the file to erase and write the statement that executed during that loop execution.
To correct this, take out the code payFile.open("payFile.txt"); from both for loops and put that just below ofstream payFile;
1 2 3
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;
1 2 3
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;
Replace the above codes with the below code payFile<<"Employee "<<(count+1)<<" you worked for "<<time[count]<<" hours at $"<<payRate<<" and earned $"<<setprecision(5)<<total<<endl;
Hope Everything works perfect now.
SUggestion: Use if-else instead of switch.It will be much easier
if(hours<=40)
{
Line 64 to Line74
}
else if(hours>40)
{
Line 85 to Line 96
}
else
Line 98