getline and cin ??

I'm stuck in a loop (lines 35-45). I just put a system PAUSE there to see where it was falling through. Is there an issue with a return character in the buffer? Is that why it's not stopping at the employee ID entry? I tried cin.ignore() and cin.clear(), but neither worked right. Any suggestions? This program is due on Tues. I'm close, but I need some help please!



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
125
126
127
128
129
130
131
132
133
134
135
136
#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++)
    {
         //enter employee name and save to array                  
         cout << "Enter the following employee info." << endl;
         cout << "First, Middle and Last name: ";
         cin.getline(name,SIZE);
         name[idx] = name[SIZE];
         
         cin.ignore();
         do //employee ID entry with input validation and save to array
         {    
         cout << "Employee ID#: ";
         cin >> id[idx];
         
         if (id[idx].size() == 6)
              break;
         else
              cout << "Invalid employee ID#. Must be 6 characters. Try again.\n";
              system ("PAUSE");
         } 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]);
    
         for(int idx = 0; idx < SIZE; idx++) //write entry to the output file.
         {
              outputFile << name[idx] << "    " << id[idx]<< "    " << rate[idx] << "    " << hours[idx];
              outputFile << "     " << myGross[idx] << "     " << myStateTax[idx] << "    " << myFedTax[idx];
              outputFile << "    " << myUnionFees[idx] << "    " << myNet[idx] << endl; 
          }
    } 
    
    //display entries to user
    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++) //write entry to the output file.
    {
         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;

}
the problem is that you read only 5 characters here
1
2
3
4
cout << "Enter the following employee info." << endl;
cout << "First, Middle and Last name: ";
cin.getline(name,SIZE);
name[idx] = name[SIZE];

Next on this line : name[idx] = name[SIZE]; you try to asign to the first char of the name array an element that does not exist. Note that name has 5 elements 0-4, on that line you do name[idx] = name[5]; that is out of the range of the array.
I don't really understand what you are trying to do. I don't think that is how you should do what you want.
ok I changed it to this, but don't I then need to save the entry into the name array? (name[idx])? I'm trying to loop through up to 5 employees and save every entry into an array. Then I have to save it to an output file and display it to the console.


1
2
3
4
//enter employee name and save to array                  
         cout << "Enter the following employee info." << endl;
         cout << "First, Middle and Last name: ";
         cin.getline(name,45);
My question should probably be, how do I save getline entries (frist MI last name) into an array for up to 5 entries?
In the code you did you can't store more then 5 characters.
Better store them streight in the string, or increase the size of the name char array.
Name can only hold a sequence of 5 chars.
The simplest way would be to have a for loop with a string array and a buffer declared outside the for loop.
Something like this
1
2
3
4
5
6
7
string names[5];
char name[45]; // number of chars that the full name can have
for(int i = 0; i < 5; i++)
{
  getline(name, 45);
  names[i].assign(name);
}


At the end of the loop names will have all the 5 names, and you could print them like so
cout << names[i]; // i = position in the index 0-5
Last edited on
1
2
3
4
5
do
{
// whatever
}
while (1);

just curious.. is this typical program structure and if so wtf does it do?
Last edited on
That is a infinite loop. That is also something that i don't think should be used in this case. You usually use this construct when you have something that you know needs to go "endleslly". This type are usually used by game lops for example, or by servers(a simple socket server will use this type of loop to check for new connection/data, and act acordingly).
You might want to change that to something better like
1
2
3
4
do
{
// whatever
}while(condition); //this could be for example a check to see if you finished reading or something.  
Last edited on
ok lol I'm not gonna change it to anything, because I didn't write it, that's from the op ^

thanks for clearing up
I know, the sugestions ware for the op :P. I skimed to fast over the code first time and i didn't see the infinit loop to tell him the things i did in the previous post :P
Hi guys. Thanks for the suggestions! I changed lines 34-35 to what you suggested. Hopefully that will improve it. The "}while(1)" seems to work for now, so I don't want to mess with it. I got that suggestion from another forum I think.

So it compiles ok and the program runs through ok for the first loop, but on the 2nd loop, it skips the name and jumps right to asking for the employee ID#. I can't figure this out. Help!!


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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 names[SIZE], id[SIZE];
    char name[45], answer;
    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");
    
    do
    {
         for(int idx = 0; idx < SIZE; idx++)  //for loop to iterate through questions for several employees
         {
         //enter employee name and save to array                  
              cout << "Enter the following employee info." << endl;
                  
              cout << "First, Middle and Last name: ";
              cin.getline(name,45);
              names[idx].assign(name);

              do //employee ID entry with input validation and save to array
              {    
              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 << "Do you have another employee to enter? (Y or N) ";
         cin >> answer;
         if (answer == 'Y')
              break;
         else
              idx = SIZE;
         } 
        
    } while(answer == 'Y' || answer == 'y');
    
    
    //display entries to user
    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++) //write entry to the output file.
    {
         cout << name[idx] << "    " << id[idx]<< "    " << rate[idx] << "    " << hours[idx];
         cout << "     " << myGross[idx] << "     " << myStateTax[idx] << "    " << myFedTax[idx];
         cout << "    " << myUnionFees[idx] << "    " << myNet[idx] << endl; 
    }
    
    for(int idx = 0; idx < SIZE; idx++) //write entry to the output file.
    {
         outputFile << name[idx] << "    " << id[idx]<< "    " << rate[idx] << "    " << hours[idx];
         outputFile << "     " << myGross[idx] << "     " << myStateTax[idx] << "    " << myFedTax[idx];
         outputFile << "    " << 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;

}
oh and I was also toying with the idea of adding the lines 104-112. Still have to figure the proper placement of that.
You can remove the for loop alltogheter, it's not being used anyway, you just need to add another check to the "while(answer == 'Y' || answer == 'y');"
Also, the problem is that the newline(\n) character still resides in the cin buffer. so you need to call cin.ignore() right before you exit the loop. i added that right before the break.
Also, the problem is that the newline(\n) character still resides in the cin buffer. so you need to call cin.ignore() right before you exit the loop. i added that right before the break.


right before exiting which loop? which line is the break you are referring to?
I tried a different way to handle the name by breaking it up cause I couldn't figure it out. Seems to work ok although I don't have any validation on the name. But the output looks wrong still. I'll try the cin.ignore() you suggested on the previous version if you can tell me which line you meant, nedo.


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#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 fname[SIZE], lname[SIZE], mname[SIZE], id[SIZE];
    char answer;
    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");
    
    do
    {
         for(int idx = 0; idx < SIZE; idx++)  //for loop to iterate through questions for several employees
         {
                               
              cout << "Enter the following employee info." << endl;
              //enter employee first name and save to array 
              cout << "First name: ";
              cin >> fname[idx];
              //enter employee middle name and save to array
              cout << "Middle Initial: ";
              cin >> mname[idx];
              //enter employee last name and save to array
              cout << "Last name: ";
              cin >> lname[idx];

              do //employee ID entry with input validation and save to array
              {    
              cout << "Employee ID#: ";
              cin >> id[idx];
              //make sure id is six characters long
              if (id[idx].size() == 6)
                   break;
              else
                   cout << "Invalid employee ID#. Must be 6 characters. Try again.\n";
              } while(1); //if it's not six characters long, loop the question again
    
              do //get pay rate with input validation
              {
              cout << "Hourly Rate: $";
              cin >> rate[idx];
              //make sure pay rate is a positive number
              if (rate[idx] > 0)
                   break;
              else
                   cout << "Invalid pay rate. Must be > $0.00. Try again.\n ";
              } while (1); //if pay rate is not a positive number, loop the question again
    
              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);//if not valid entry, loop the question again.
              //if levels 1 or 2, then ask OT hours.
              while (level[idx] == 1 || level[idx] == 2)
              {
                   cout << "Overtime Hours Worked: ";
                   cin >> hours[idx];
                   if (level[idx] == 1)//if level 1, calculate hours
                   {
                        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;
                             cout << "Your OT hours will be calculated at 20 hours." << endl;
                             hours[idx] = 20;
                             break;
                        }
                        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;
                             cout << "Your OT hours will be calculated at 15 hours." << endl;
                             hours[idx] = 15;
                             break;
                        }
                        else 
                             cout << "Invalid entry. Must be between 0 - 15." << endl;
                   }
              if(level[idx] == 3)
                   hours[idx] = 0;
              }

         cout << endl << endl;
    
    
         myGross[idx] = rate[idx] * 40 + (hours[idx]) * 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;
         cout << endl;
         */
         
         cout << "Do you have another employee to enter? (Y or N) ";
         cin >> answer;
         if (answer == 'Y')
              break;
         else
              idx = SIZE;
         } 
        
    } while(answer == 'Y' || answer == 'y'); 
    cout << endl << endl;
    
    //display entries to user
    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++) //step through the array and write each entry to the console.
    {
         cout << fname[idx] << " " << mname[idx] << " " << lname[idx] << "    " << id[idx];
         cout << "    " << rate[idx] << "    " << hours[idx] << "     " << myGross[idx];
         cout << "     " << myStateTax[idx] << "    " << myFedTax[idx] << "    ";
         cout << myUnionFees[idx] << "    " << myNet[idx] << endl; 
    }
    
    for(int idx = 0; idx < SIZE; idx++) //step through the array and write each entry to the output file.
    {
         outputFile << fname[idx] << " " << mname[idx] << " " << lname[idx] << "    " << id[idx];
         outputFile << "    " << rate[idx] << "    " << hours[idx] << "     " << myGross[idx];
         outputFile << "     " << myStateTax[idx] << "    " << myFedTax[idx] << "    ";
         outputFile << 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;

}
Sorry for not specifiyng. I was refering to the main loop.
To change from
1
2
if (answer == 'Y')
              break;

to
1
2
3
4
5
6
if (answer == 'Y')
{
              cin.ignore();
              cin.clear();
              break;
}


I also was refearing to the main for loop
for(int idx = 0; idx < SIZE; idx++)
Is redundant. You know you have to introduce a max of 5 names, right?
So add a counter at the beginning of the first do loop and change the last while to
 
while((answer == 'Y' || answer == 'y') && (counter <= SIZE)); 
Last edited on
Topic archived. No new replies allowed.