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


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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <fstream>

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.




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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <fstream>

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?


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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <fstream>

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

There's a handy post about this: http://www.cplusplus.com/forum/articles/6046/

FURTHER EDIT: Still, I'd find another way to do those loops. You can avoid those break statements completely with a good while condition. It will also, in my opinion, tidy the code up considerably.
Last edited on
Topic archived. No new replies allowed.