### stuck on loop and logic HELP PLEASE

Writing a payroll program due next week. I'm stuck on a loop when the user enters the employee level (lines 49-57).

But ALSO, I'm stuck on the logic when I factor in this level to figure overtime hours worked. These are the levels below. But I keep going back and forth on best way to do this. Nested for sure, but how? I started something but then had to comment it out cause it wouldn't compile. Not even sure if I was going in the right direction. PLEASE HELP!

level 1 - can only work up to 20 OT hours
level 2 - up to 15 OT hours
level 3 - no OT allowed

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include #include #include #include using namespace std; int main() { const double STATETAX = .05; const double FEDTAX = .15; const double UNIONFEES = .02; const double OTRATE = 1.5; string id[10]; char name[45], level[10]; int idx = 0, hours[10]; double rate[10], myGross[10], myStateTax[10], myFedTax[10], myUnionFees[10]; double myNet[10]; ofstream outputFile; outputFile.open("E:\\CIS265\\PayrollProgram.txt"); cout << "Enter the following employee info." << endl; cout << "First, Middle and Last name: "; cin.getline(name,45); do //employee ID entry with input validation { cout << "Employee ID#: "; cin >> id[idx]; if (id[idx].size() == 6) break; else cout << "Invalid employee ID#. Must be 6 characters. Try again.\n"; } while (1); do //get pay rate with input validation { cout << "Hourly Rate: \$"; cin >> rate[idx]; if (rate[idx] > 0) break; else cout << "Invalid pay rate. Must be > \$0.00. Try again.\n "; } while (1); do //enter Level with validation { cout << "Employee Level (1, 2 or 3): "; cin >> level[idx]; if (level[idx] >= 1 && level[idx] <=3) break; else cout << "Invalid Entry. Level must be 1, 2, or 3. Try again." << endl; } while (1); /* while (level[idx] == 1 || level ==2) { cout << "Total Hours Worked: "; cin >> hours[idx]; if (level[idx] == 1) { if (hours[idx] >= 0 && hours[idx] <= 60) break; else if (hours[idx] > 60) cout << "You cannot work more than 60 hours at Level 1." << endl; else cout << "Invalid entry. Must be between 0 - 60."; } else if (level[idx] == 2) { if (hours[idx] >= 0 && hours[idx] <= 55) break; else if (hours[idx] > 55) cout << "You cannot work more than 55 hours at Level 2." << endl; else cout << "Invalid entry. Must be between 0 - 55."; } } */ myGross[idx] = rate[idx] * 40 + (hours[idx] - 40) * 1.5; myStateTax[idx] = myGross[idx] * STATETAX; myFedTax[idx] = myGross[idx] * FEDTAX; myUnionFees[idx] = myGross[idx] * UNIONFEES; myNet[idx] = myGross[idx] - (myStateTax[idx] + myFedTax[idx] + myUnionFees[idx]); system("PAUSE"); return EXIT_SUCCESS; }``````
in terms of your first question, you are using a char[] when you should be using an int array. A char will never equal an int unless you convert it into one. If you fix that, then working out the logic is just a simple comparison to see if they are allowed the OT based on the level. Good luck and please post any updates to the code if you still have questions.

I just reread my response so I wanted to add clarification. For level[] you use an array of chars. If a user enters 1, it'll actually be '1' which will never match 1. You either need to use an int arrary, or typecast level[position] to an int from a char before you do the comparison check.
Last edited on
OMG thank you thank you thank you! Now I see. I just added the single quotes to fix and it works. Well, I got past the loop at least. Tackling the rest now.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````#include #include #include #include #include #include using namespace std; int main() { const double STATETAX = .05; const double FEDTAX = .15; const double UNIONFEES = .02; const double OTRATE = 1.5; string id[10]; char name[45], level[10]; int idx = 0, hours[10]; double rate[10], myGross[10], myStateTax[10], myFedTax[10], myUnionFees[10]; double myNet[10]; ofstream outputFile; outputFile.open("E:\\CIS265\\PayrollProgram.txt"); cout << "Enter the following employee info." << endl; cout << "First, Middle and Last name: "; cin.getline(name,45); do //employee ID entry with input validation { cout << "Employee ID#: "; cin >> id[idx]; if (id[idx].size() == 6) break; else cout << "Invalid employee ID#. Must be 6 characters. Try again.\n"; } while (1); do //get pay rate with input validation { cout << "Hourly Rate: \$"; cin >> rate[idx]; if (rate[idx] > 0) break; else cout << "Invalid pay rate. Must be > \$0.00. Try again.\n "; } while (1); do //enter Level with validation { cout << "Employee Level (1, 2 or 3): "; cin >> level[idx]; if (level[idx] == '1' || level[idx] == '2' || level[idx] == '3') break; else cout << "Invalid Entry. Level must be 1, 2, or 3. Try again." << endl; } while (1); while (level[idx] == '1' || level[idx] == '2') { cout << "Overtime Hours Worked: "; cin >> hours[idx]; if (level[idx] == '1') { if (hours[idx] >= 0 && hours[idx] <= 20) break; else if (hours[idx] > 20) cout << "You cannot work more than 20 overtime hours at Level 1." << endl; else cout << "Invalid entry. Must be between 0 - 20."; } else if (level[idx] == '2') { if (hours[idx] >= 0 && hours[idx] <= 15) break; else if (hours[idx] > 15) cout << "You cannot work more than 15 overtime hours at Level 2." << endl; else cout << "Invalid entry. Must be between 0 - 15."; } } myGross[idx] = rate[idx] * 40 + (hours[idx] - 40) * 1.5; myStateTax[idx] = myGross[idx] * STATETAX; myFedTax[idx] = myGross[idx] * FEDTAX; myUnionFees[idx] = myGross[idx] * UNIONFEES; myNet[idx] = myGross[idx] - (myStateTax[idx] + myFedTax[idx] + myUnionFees[idx]); cout << "Gross: " << myGross[idx] << endl; cout << "State Tax: " << myStateTax[idx] << endl; cout << "Fed Tax: " << myFedTax[idx] << endl; cout << "Union Fees: " << myUnionFees[idx] << endl; cout << "Ny Net: " << myNet[idx] << endl; system("PAUSE"); return EXIT_SUCCESS; }``````
Stuck again :( I had the code right, but then I added the for loop to allow for up to 5 employees and now I'm stuck in the loop starting at line 33 and cant get out. Ideas?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` ``````#include #include #include #include #include #include using namespace std; int main() { const double STATETAX = .05; const double FEDTAX = .15; const double UNIONFEES = .02; const double OTRATE = 1.5; const int SIZE = 5; string id[SIZE]; char name[SIZE]; int hours[SIZE], level[SIZE]; double rate[SIZE], myGross[SIZE], myStateTax[SIZE], myFedTax[SIZE], myUnionFees[SIZE]; double myNet[SIZE]; ofstream outputFile; outputFile.open("E:\\CIS265\\PayrollProgram.txt"); for(int idx = 0; idx < SIZE; idx++) { cout << "Enter the following employee info." << endl; cout << "First, Middle and Last name: "; cin.getline(name,SIZE); do //employee ID entry with input validation { cout << "Employee ID#: "; cin >> id[idx]; if (id[idx].size() == 6) break; else cout << "Invalid employee ID#. Must be 6 characters. Try again.\n"; } while (1); do //get pay rate with input validation { cout << "Hourly Rate: \$"; cin >> rate[idx]; if (rate[idx] > 0) break; else cout << "Invalid pay rate. Must be > \$0.00. Try again.\n "; } while (1); do //enter Level with validation { cout << "Employee Level (1, 2 or 3): "; cin >> level[idx]; if (level[idx] == 1 || level[idx] == 2 || level[idx] == 3) break; else cout << "Invalid Entry. Level must be 1, 2, or 3. Try again." << endl; } while (1); while (level[idx] == 1 || level[idx] == 2) { cout << "Overtime Hours Worked: "; cin >> hours[idx]; if (level[idx] == 1) { if (hours[idx] >= 0 && hours[idx] <= 20) break; else if (hours[idx] > 20) cout << "You cannot work more than 20 overtime hours at Level 1." << endl; else cout << "Invalid entry. Must be between 0 - 20." << endl; } else if (level[idx] == 2) { if (hours[idx] >= 0 && hours[idx] <= 15) break; else if (hours[idx] > 15) cout << "You cannot work more than 15 overtime hours at Level 2." << endl; else cout << "Invalid entry. Must be between 0 - 15." << endl; } } cout << endl << endl; myGross[idx] = rate[idx] * 40 + (hours[idx] - 40) * 1.5; myStateTax[idx] = myGross[idx] * STATETAX; myFedTax[idx] = myGross[idx] * FEDTAX; myUnionFees[idx] = myGross[idx] * UNIONFEES; myNet[idx] = myGross[idx] - (myStateTax[idx] + myFedTax[idx] + myUnionFees[idx]); } cout << "\t\t\tCompany A B C" << endl; cout << "\t\t\t= = = = = = =" << endl; cout << "\t\t\tPay Roll Program" << endl; cout << "======================================================================"; cout << "==========" << endl; cout << "Name " << "Id# " << "Rate/h " << "OT hours " << "Gross "; cout << "State Tax " << "Fed Tax " << "Union Fees " << "Net " << endl; cout << "==== " << "=== " << "====== " << "======== " << "===== "; cout << "========= " << "======= " << "========== " << "==== " << endl; for(int idx = 0; idx < SIZE; idx++) { cout << name[idx] << " " << id[idx]<< " " << rate[idx] << " " << hours[idx]; cout << " " << myGross[idx] << " " << myStateTax[idx] << " " << myFedTax[idx]; cout << " " << myUnionFees[idx] << " " << myNet[idx] << endl; } /* cout << "Gross: " << myGross[idx] << endl; cout << "State Tax: " << myStateTax[idx] << endl; cout << "Fed Tax: " << myFedTax[idx] << endl; cout << "Union Fees: " << myUnionFees[idx] << endl; cout << "Ny Net: " << myNet[idx] << endl; */ system("PAUSE"); return EXIT_SUCCESS; }``````
Yeah, your while condition needs changing.

A do-while loop will iterate so long as the while condition is met. Your current while condition is 1. This is interpreted by the condition as a boolean evaluation, whereby zero is false and anything non-zero is true.

Therefore, the line `while( 1 );` will always evaluate to true and your loop will run indefinitely.
but it worked perfectly before I added the for loop on line 26 to be able to loop through up to 5 employees. How could that affect the nested loops? I didn't touch them. Is there a problem with a do while within a for loop?
Ah, my mistake, I didn't see your break statements in there. Let me have another glance...

EDIT: Oh, I see. It's most likely that you're mixing `cin.getline()` and `cin >>`.