### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101`` ``````#include #include #include 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."<>time[count]; hours=time[count]; cout<>payRate; cout<>payRate; cout<
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 "<
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:

 ``1234567891011121314`` ``````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.