Incorrect output

I am having some trouble displaying data that is in a structure. After the user enters all their information, my program is supposed to display what was entered in the form of ... FirstName LastName TotalPay in the same line. I am testing it out with the hourly workers and their pay, Say there is 3 employees, what is displaying is the last employees information for 3 times. How can I fix this or what am I doing wrong?




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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
#include <iomanip> 

using namespace std;


//Create enumeration that contains bonus information 
enum BonusAvailability{NO_BONUS, AVG_BONUS, HIGH_BONUS};


//Create a struct that contains the employees personal information
struct PersonalInfo
{
	string fName,
		   lName,
		   title;
};

//Create a struct for hourly workers, calls personal info struct 
struct HourlyW
{
	PersonalInfo pData ; 
	double hoursWorked; 
	double hourlyRate;
};

//Create a struct for salary workers, calls personal info struct
struct SalaryW
{
	PersonalInfo pData;
	double salary;
	double bonus;
	BonusAvailability bData;
	
};



int main()
{
	
	
	int numInfo; // Holds number of employess to be entered 
	int numHour; // Holds number of hourly workers 
	int numSalary; // Holds number of salary workers
	char selection; // If user wants to continue 
	char selection2; // If user wants to continue 
	double tHour; // Total hourly 
	double tSalary; // Total salary
	HourlyW employee; // Create employee under Hourly W
	SalaryW emp; // Create emp under Salary W
	BonusAvailability b; // Create b under Bonus Availability 
	
				  // Get number of employees to be entered 
				  cout << "Enter the number of employees you will be entering information for: ";
				  cin >> numInfo;
				  
				  // If number is negative, show error message  
				  while (numInfo < 0)
				   {
					   cout << "Invalid number or negative number! Try again!" << endl;
					   exit(1);
				   }
				  // Get number of hourly workers 	  
				  cout << "How many are hourly workers?" << endl;
				  cin >>numHour;
				  
				  // If number is negative, show error message
				  while (numHour < 0)
				   {
					   cout << "Invalid number or negative number! Try again!" << endl;
					   exit(1);
				   }
				  
				  // Get number of salary workers 
				  cout << "How many are salary workers?" << endl;
				  cin >> numSalary;
				  
				  // If number is negative, show error message 
				  while (numSalary < 0)
				   {
					   cout << "Invalid number or negative number! Try again! " << endl;
					   exit(1);
				   }
				   
				   // If sum of hourly and salary is less than or greater than the total number of employees entered show error message 
				   while ((numHour + numSalary) > numInfo || (numHour + numSalary < numInfo))
				   {
					   cout << "Hourly workers plus Salary workers does not equal " << numInfo << endl;
					   cout << "Try again " << endl;
					   cout << "" << endl;
					   cout << "How many hourly workers?" << endl;
					   cin >>numHour;
					   while (numHour < 0)
						{
							cout << "Invalid number or negative number!" << endl;
							exit(1);
						}
						cout << "How many are salary workers?" << endl;
						cin >> numSalary;
						while (numSalary < 0)
							{
								cout << "Invalid number or negative number!" << endl;
								exit(1);
							}
						if (numHour + numSalary == numInfo)
						{
							break;
						}
				   }
				 
				  
				  // If hourly workers is greater than 0, run this to get the information
				  if (numHour > 0)
				  {
					cout << "Hourly Workers!" << endl;
					cout << "---------------" << endl;
				  
					for (int i = 0; i < numHour; i++)
					{
						  
					  
					  cout << "Enter first name: ";
					  cin >> employee.pData.fName;
					  cout << "" << endl;
					  cout << "Enter last name: ";
					  cin >> employee.pData.lName;
					  cout << "" << endl;
					  cout << "Enter their title: ";
					  cin >> employee.pData.title;
					  cout << "" << endl;
					  cout << "Enter the hours worked: ";
					  cin >> employee.hoursWorked; 
					  
					  // If hours worked is less than 0 or greater than 80, show error message 
					  while (employee.hoursWorked < 0 || employee.hoursWorked > 80)
					  {
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter the hours worked: ";
						  cin >> employee.hoursWorked;
						  if (employee.hoursWorked > 0 || employee.hoursWorked < 80)
							  break;
					  }
					  cout << "" << endl;
					  cout << "Enter the hourly rate: ";
					  cin >> employee.hourlyRate;
					  cout << "" << endl;
					  tHour = employee.hoursWorked * employee.hourlyRate; // Get total hourly pay 
					  
					  //Ask if they want to continue 
					  cout << "Would you like to continue? Y or N" << endl;
					  cin >> selection;
					  if (selection == 'y' || selection == 'Y')
					  {
						  continue;
					  }
					  else 
					  {
						  break;
					  }
					 // cout << "Total " << tHour << endl;  //TEST 
					  
				    
				    } 
			      }
				  
				  
				 // If number of salary workers is greater than 0, run this to get the information  
				 if (numSalary > 0)
				 {
					cout << "Salary Workers!" << endl;
					cout << "---------------" << endl;
					for (int i = 0; i < numSalary; i++)
					{
						
						cout << "Enter first name: ";
						cin >> emp.pData.fName;
						cout << "" << endl;
						cout << "Enter last name: ";
						cin >> emp.pData.lName;
						cout << "" << endl;
						cout << "Enter their title: ";
					    cin >> emp.pData.title;
					    cout << "" << endl;
						cout << "Enter salary: ";
						cin >> emp.salary;
						
						// If salary is less that 0, show error message 
						while (emp.salary < 0)
						{
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter the salary: ";
						  cin >> emp.salary;
						  if (emp.salary > 0)
							  break;
						}
					  
						cout <<"" << endl;
						cout << "Enter bonus: ";
						cin >> emp.bonus;
						
						// If bonus is less than 0, show error message 
						while (emp.bonus < 0)
					    {
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter bonus: ";
						  cin >> emp.bonus;
						  if (emp.bonus > 0)
						  {
							break;
						  }
						}
						
						
						tSalary = emp.salary + emp.bonus; // Hold total pay for salary workers 
						cout << "Salary is " << tSalary << endl; //TEST 
						
						cout << "" << endl;
						
						//See if user wants to continue 
					    cout << "Would you like to continue? Y or N" << endl;
					    cin >> selection2;
					    if (selection2 == 'y' || selection2 == 'Y')
							{
								continue;
							}
						else 
							{
								break;
							}
					  
					  // See what category the salary workers fall under 
					  if (emp.bonus == 0)
					  {
						  b = static_cast<BonusAvailability> (0);
						 
					  }
					  else if  (emp.bonus > 0 && emp.bonus <= 5000)
					  {
						  b = static_cast<BonusAvailability> (1);
					  }
					  else if (emp.bonus > 5000)
					  {
						  b = static_cast<BonusAvailability>(2);
					  }
						cout << "" << endl;
						cout << "Bonus is " << b << endl; //TEST 
					}
					
				 }
				 
				// Begin displaying information 
				for (int i = 0; i < numInfo; i++)
				{
					cout << "List of employees" << endl;
					cout << "------------------" << endl;
					cout << employee.pData.fName << " " <<  employee.pData.lName << " "    << tHour << endl;
				}
				 
return 0;				 
	
}


Last edited on
In order to store data in more than one object, you must have more than one object and you must access those different objects (perhaps contained in an array or vector.) On the loop that begins on line 263, you output the data from the same object numInfo times.
Would creating an array for a structure be something like ?


1
2
HourlyW employee[NUM_HOUR]; 
SalaryW emp[NUM_SALARY]; 


Hi Marie,

Arrays of Structures

Structures may be arrayed. In fact, structure arrays are quite common. To declare an
array of structures, you must first define a structure, then declare an array of its type.
For example, to declare a 100-element array of structures of type inv_type (defined
earlier), you would write

inv_type invtry[100];

To access a specific structure within an array of structures, you must index the
structure name. For example, to display the on_hand member of the third structure,
you would write

cout << invtry[2].on_hand;

Like all array variables, arrays of structures begin their indexing at zero.
From : C++ From the ground up 3rd edition

Now I'm new to c++, please keep that in mind, I may not be 100% accurate here...
Arrays have random access. That's a good thing, You can access any element at anytime, but they are static, and can't be resized. If your array is set to ten, and 11 employees need to store data you're kinda screwed. Vectors are dynamic. Like arrays they have random access too, plus the ability to be resized. This may be a better choice for you...
Last edited on
Topic archived. No new replies allowed.