Classes with more than one object

I am working on an overtime pay project, and I thought I was doing well. Unfortunately, my program is not displaying what it should. It's not completed yet, but it is a problem with having more than one object from a single class.

In my book, it says that you have to list a second object's attributes in terms of the first. I am not sure how to do that when the program prompts the user to enter in all the values.

OK, I have a class called CEmployee that looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CEmployee {
public:
	void ImplementCalculations(string EmployeeName, float m_hours, float m_wage);
	void DisplayEmployInformation(void);
	void Addsomethingup (CEmployee Employ1, CEmployee Employ2);
	string EmployeeName ;
	float m_hours ;
	float m_wage ;
	float m_basepay ;
   	float m_overtime_hours ;
	float m_overtime_wage ;
	float m_overtime_pay ;
	float m_salary ;
	float m_iTotal_salaries ;
	float m_iIndividualSalary ;
	int m_iTotal_hours ;
	int m_iTotal_OvertimeHours ;
};


I prompted a user to enter 3 employees, objects I declared as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	CEmployee Employ1;
	CEmployee Employ2;
	CEmployee Employ3;

	cout << "\n\nEnter the first employee's name = ";
	cin >> Employ1.EmployeeName;
	cout << "Enter the hours worked  = ";
	cin >> Employ1.m_hours ;
	cout << "Enter his or her hourly wage = ";
	cin >> Employ1.m_wage ;

	cout << "\n\nEnter the second employee's name = ";
	cin >> Employ2.EmployeeName;
	cout << "Enter the hours worked  = ";
	cin >> Employ2.m_hours ;
	cout << "Enter his or her hourly wage = ";
	cin >> Employ2.m_wage ;

	cout << "\n\nEnter the third employee's name = ";
	cin >> Employ3.EmployeeName;
	cout << "Enter the hours worked  = ";
	cin >> Employ3.m_hours ;
	cout << "Enter his or her hourly wage = ";
	cin >> Employ3.m_wage ;


Does this look correct so far or am I way off base here?

Last edited on
That looks lovely. What's it doing that you don't like?
Well, it's the way the numbers are being displayed.

It is supposed to display this:

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
Welcome to the Employee Pay Center 

Enter the employee name = John 
Enter the hours worked = 44 
Enter his or her hourly wage = 3.33 

Enter the employee name = Mary 
Enter the hours worked = 33 
Enter his or her hourly wage = 2.22 

Enter the employee name = Mark 
Enter the hours worked = 29 
Enter his or her hourly wage = 2.22 

Employee Name ............. = John 
Base Pay .................. = 133.20 
Hours in Overtime ......... = 4 
Overtime Pay Amount........ = 19.98 
Total Pay ................. = 153.18 

Employee Name ............. = Mary 
Base Pay .................. = 73.26 
Hours in Overtime ......... = 0 
Overtime Pay Amount........ = 0.00 
Total Pay ................. = 73.26 

Employee Name ............. = Mark 
Base Pay .................. = 64.38 
Hours in Overtime ......... = 0 
Overtime Pay Amount........ = 0.00 
Total Pay ................. = 64.38 


but the numbers for the base pay (only for the 2nd and third Employ) are being displayed like this:

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
Welcome to the Employee Pay Center 

Enter the employee name = John 
Enter the hours worked = 44 
Enter his or her hourly wage = 3.33 

Enter the employee name = Mary 
Enter the hours worked = 33 
Enter his or her hourly wage = 2.22 

Enter the employee name = Mark 
Enter the hours worked = 29 
Enter his or her hourly wage = 2.22 

Employee Name ............. = John 
Base Pay .................. = 133.20 
Hours in Overtime ......... = 4 
Overtime Pay Amount........ = 19.98 
Total Pay ................. = 153.18 

Employee Name ............. = Mary 
Base Pay .................. = 4.99142e+034
Hours in Overtime ......... = 0 
Overtime Pay Amount........ = 0.00 
Total Pay ................. = 73.26 

Employee Name ............. = Mark 
Base Pay .................. = 1.7402e-039
Hours in Overtime ......... = 0 
Overtime Pay Amount........ = 0.00 
Total Pay ................. = 64.38 




http://www.cplusplus.com/forum/beginner/5381/

jsmith says:
cout << fixed << setprecision( 2 ) << a << endl;

is that what you are looking for?

I know it's a cout flag, but don't remember and don't have my compiler handy. They are displaying that way because there are too many digits so it's showing sci...
lol,,, actually reading the results... are you dividing by a number approaching zero or something? the ones that have that output have zeros under them... make sure of your math also, just to be safe.. something to do with overtime, but don't see the rest of the code
Ahhhh.....I think I have it.

The amount set to be displayed as Base Pay is the variable named m_basepay, but this variable isn't set to be displayed.

I have an if statement:

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
	//Initialize overtime variables
	m_overtime_hours=0;
	m_overtime_wage=0;
	m_overtime_pay=0;

	if (m_hours > 40) 
	{	
		// Calculate base pay by multiplying the wage by 40
		m_basepay = 40*m_wage;	
		// Calculate overtime hours by subtracting 40 from the hours worked
		m_overtime_hours = (m_hours-40);
		// Calculate overtime wage by multiplying the wage by 1.5
		m_overtime_wage = m_wage*1.5;
		// Calculate the overtime pay by multiplying the overtime wage by the overtime hours
		m_overtime_pay = m_overtime_hours*m_overtime_wage;
		// Calculate this employee's total salary by adding the base pay and the overtime pay
		m_salary = m_overtime_pay + m_basepay;
		
		DisplayEmployInformation();

	}	// End if (m_hours > 40)
	else
	{
		// Calculate the salary by multiplying the hours by the wage
		m_salary = m_hours*m_wage;
		DisplayEmployInformation();

	} // End of the else 


In the else portion, the base pay isn't set...only the salary. I have to add this:

m_basepay = m_hours*m_wage;
cool... glad you found it.
OK, new question. How does one send these objects to a function where the hours, overtime, and total salary can be summed?
you caught me as I was signing out... do you mean how do you make the function?

if you don't want to allow the function to change something it could be (void can be changed if you want to return something also...). Sending as a reference to avoid creating another var, but if you need a copy that doesn't change the send var, then you can remove both the "const" and "&"..

1
2
3
void nameOfFunction( const CEmployee & ce ) {
  // do something with ce
}


and if you want to make changes

1
2
3
void nameOfFunction( CEmployee & ce ) {
  // do something with ce
}


is that what you are looking for?

Last edited on
then you could call it like this

 
nameOfFunction(Employ1); // to do things with employ1.. 


you could send more variables also by separating the sent vars with a comma

.. in creating and calling the function
Last edited on
You guys are great! Thank you! That helped tremendously!

One last question:

Is it possible to copy/paste the results of my console app or am I stuck screenshotting it?
In general terms, it depends what you're using as your console. In specific terms, if you're using the standard Windows console, or one of the standard *nix consoles, then yes.

I'm guessing you're using windows; as I recall, up under one of the menu options is something like "Mark". If you select that, you can then highlight all the text, and if you press Enter, it'll be copied to the clipboard.

However, for bonus points, put all your output to a log file as well as to the stdout. :)

Last edited on
Topic archived. No new replies allowed.