Error with value being returned

Hello guys, I am working on my final project for my class and after finally getting it to compile with no errors thanks to finding examples/tutorials and following skeleton code I cam encountering a problem.

The program runs, asks all the correct questions but when it displays the base pay and total pay for all 3 employees it comes back as ( -1.0743 blah blah )

I was really hoping someone could give it a quick look and tell me whats going wrong ?

When they work over 40 hours it works correctly but when they work under 40 hours it displays those weird numbers in those sections.

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
// Kevin Johnson -- Overtime Pay -- Final Assignment
// Created 11/14/2013   // Edited 11/17/2013



#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

int _tmain(int argc, _TCHAR* argv[])  // Code to eliminate errors in Express with no Pre-Compiled Header
{
	return 0;
}


using namespace std;


//Declaring

class EmpClass 
{

public: // Public Access

	void ImplementCalculations(string EmployeeName, int hours, double wage);

	void DisplayEMPinfo(void);

	void Calcs (EmpClass, EmpClass, EmpClass);


	// EmployeeName String
	string EmployeeName;

	// Totals
	float TotSal;
	int TotHours;
	int TotalOThour;


	// Individual
	float indSal;


	// Stats & Overtime
	int hours;
	float wage;
	float basepay;
   	int ot_Hour;
	float ot_Pay;
	float ot_Xtra;

};

int main()
{	system("cls"); 

// Title Display

	cout << "\nPay Calculation Software V 1.00a\n\n" ;



// Employee's  1-3
	// Storing Data For Each Employee
	// Prompted Information Set 1 saves to Emp1 , Set 2 saves to emp2 and so on...



	EmpClass Emp1;
	EmpClass Emp2;
	EmpClass Emp3;

// Collect information (Prompt)

	cout << "\n\nPlease Enter The First Employee Name::::::: = ";
	cin >> Emp1.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp1.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp1.wage;
	system("cls");

	/// Second Prompt (second employee)

	cout << "\n\nPlease Enter The Second Employee Name::::::: = ";
	cin >> Emp2.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp2.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp2.wage;
	system("cls");

	// Third Prompt ( third employee)

	cout << "\n\nPlease Enter The Third Employee Name::::::: = ";
	cin >> Emp3.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp3.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp3.wage;
	system("cls");
	cout << endl;


	/// Calculations   (( Formatting the calculations for Employees 1-3

	Emp1.ImplementCalculations(Emp1.EmployeeName, Emp1.hours, Emp1.wage);
	Emp2.ImplementCalculations(Emp2.EmployeeName, Emp2.hours, Emp2.wage);
	Emp3.ImplementCalculations(Emp3.EmployeeName, Emp3.hours, Emp3.wage);

// Return
	cin.get();
	cin.get();
	return 0;

} 


void EmpClass::ImplementCalculations (string EmployeeName, int hours, double wage)

{

// Overtime Place Holders (0)
ot_Hour=0;
ot_Pay=0;
ot_Xtra=0;



if (hours > 40)  // Determine If Overtime Required
	{		
// If hours > 40    Calculate Overtime Pay
	basepay = 40 * wage;
	ot_Hour = hours - 40;
	ot_Pay = wage * 1.5;
	ot_Xtra = ot_Hour * ot_Pay;
	indSal = ot_Xtra + basepay;


DisplayEMPinfo();

	}	
	else





	
	DisplayEMPinfo();
} 


void EmpClass::DisplayEMPinfo () {

	// Display Results  ( Formatted as requested in syllabus )

	cout << "Employee Name ......................... " << setw(15) << EmployeeName <<endl;
	cout << "Base Pay .............................. " << setw(15) << basepay << endl;
	cout << "Hours in Overtime ..................... " << setw(15) << ot_Hour << endl;
	cout << "Overtime Pay Amount ................... " << setw(15) << ot_Xtra << endl;
	cout << "Total Pay ............................. " << setw(15) << indSal << endl << endl;
}



void EmpClass::Calcs (EmpClass Emp1, EmpClass  Emp2, EmpClass Emp3)
{
	TotHours = Emp1.hours + Emp2.hours + Emp3.hours;
	TotSal = Emp1.indSal + Emp2.indSal + Emp3.indSal;
	TotalOThour = Emp1.ot_Hour + Emp2.ot_Hour + Emp3.ot_Hour;

	// Display Totals ( Formatted as requested in syllabus )

	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	cout << "%%%%%%%%%%%%%%%% EMPLOYEE SUMMARY DATA %%%%%%%%%%%%%%%%" << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	cout << "%%%% Total Employee Salaries ........ = " << setw(15) << TotSal << endl;
	cout << "%%%% Total Employee Hours ........... = " << setw(15) <<TotHours << endl;
	cout << "%%%% Total Overtime Hours ........... = " << setw(15) <<TotalOThour << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;



	system("pause"); // System Pause To View During Debugging / Editing

	}
Lines 149-160 may be a bit off, I am now really confused with why I have two displays there. I forget why there is two.
Maybe a little formatting will help clear your confusion:
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if (hours > 40)  // Determine If Overtime Required
{       
    // If hours > 40    Calculate Overtime Pay
    basepay = 40 * wage;
    ot_Hour = hours - 40;
    ot_Pay = wage * 1.5;
    ot_Xtra = ot_Hour * ot_Pay;
    indSal = ot_Xtra + basepay;


    DisplayEMPinfo();

}   
else
    DisplayEMPinfo();

The problem is that in the else part, you call DisplayEMPinfo() without setting the base pay or the total pay, which is why it prints garbage for those values.
Figured it out ! thanks !

One last thing, it just displays like this

Base Pay = 400
OT Pay = 170

They are displaying whole numbers, how do i get it to display money format?
Last edited on
It would be something like
139
140
141
142
143
144
145
146
147
148
149
if (hours > 40)
{
    // This is all fine
}
else
{
    // You need to set 'basepay' and 'indSal' here
    // Otherwise the line below will print whatever's already
    // stored in those variables (which is most likely garbage).
    DisplayEMPinfo();
}
Yeah I figured it out I still performed the base and total calcs in the (else under 40 section )

can you tell me how I can edit it so it displays in decimal (money like format)

I know how to do it in java not c++
Try cout.precision(2); and then cout << fixed;.
The first sets it to display exactly 2 digits in the decimal part, and the second makes it display in fixed-point notation (i.e. not scientific notation).

http://www.cplusplus.com/reference/ios/fixed/
To be honest, there's a lot that can be simplified and cleaned up in your code.
Feel free to ignore this, but if you're interested in hearing a second opinion... you know...

1.) I suggest that you start over.
When you are creating the new project in the "new project" dialog, make sure to create a Win32 Console Application, not an MFC or Win32 Project.
Also, make sure to uncheck "precompiled header" and check "empty project". That way, you won't be dependant on the "stdafx.h" header, and you can omit
those ugly _tmain() lines of code.

2.) using namespace std; in global scope is not recommended. Only use the using keyword where needed (which, in your case, you don't really need to).

3.) This is nit-picky, but you could give things better names. For instance, why not rename the EmpClass to "Employee"? Why does the EmpClass member "EmployeeName" need to
restate that this is the name of the employee? We know it's an employee by virtue of the fact that that's what the class is, although that might not be obvious if we were presented
with "EmpClass".

4.) There's no reason to have class functions which take its own members as parameters. You don't even use the EmployeeName string you pass into ImplementCalculations()...

5.) Why would EmpClass have a function, which accepts three EmpClass objects (of which, it's not guaranteed that the calling instance is one of them), to perform
calculations based on their data? There's no reason for that function to exist where it does, or at all. What if I want four Employees?

6.) Use of system() is not recommended. There are better alternatives. If you like, I can share some with you.

7.) Personal preference (but possibly good practice for you), is to use some kind of container to store the Employees in main(), possibly an array or a vector.
On the other hand, I don't know what the exact requirements of this assignment are. It would, however, make things less hardcoded and, with some modification, ridiculously modular.

8.) Using cin.get() twice in a row to pause the program is a poor-mans method. Again, there are alternatives which I'm ready to describe if you're willing.
Last edited on
So now that I did this , when i enter an amount of hours past 40 it displays all the info TWICE.

I got the decimals to work though

Almost like it ignores the ELSE if hours >40

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
// Kevin Johnson -- Overtime Pay -- Final Assignment
// Created 11/14/2013   // Edited 11/17/2013



#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>


int _tmain(int argc, _TCHAR* argv[])  // Code to eliminate errors in Express with no Pre-Compiled Header
{
	return 0;
}


using namespace std;


//Declaring

class EmpClass 
{

public: // Public Access

	void ImplementCalculations(string EmployeeName, int hours, double wage);

	void DisplayEMPinfo(void);

	void Calcs (EmpClass, EmpClass, EmpClass);


	// EmployeeName String
	string EmployeeName;

	// Totals
	float TotSal;
	int TotHours;
	int TotalOThour;


	// Individual
	float indSal;


	// Stats & Overtime
	int hours;
	float wage;
	float basepay;
   	int ot_Hour;
	float ot_Pay;
	float ot_Xtra;

};

int main()
{	system("cls"); 

// Title Display

	cout << "\nPay Calculation Software V 1.00a\n\n" ;



// Employee's  1-3
	// Storing Data For Each Employee
	// Prompted Information Set 1 saves to Emp1 , Set 2 saves to emp2 and so on...



	EmpClass Emp1;
	EmpClass Emp2;
	EmpClass Emp3;

// Collect information (Prompt)


// Setting Precision For Display  (( Displays In Money Format ))
		cout.precision(2);
		cout <<fixed;


		// Prompt 
	cout << "\n\nPlease Enter The First Employee Name::::::: = ";
	cin >> Emp1.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp1.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp1.wage;
	system("cls");

	/// Second Prompt (second employee)

	cout << "\n\nPlease Enter The Second Employee Name::::::: = ";
	cin >> Emp2.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp2.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp2.wage;
	system("cls");

	// Third Prompt ( third employee)

	cout << "\n\nPlease Enter The Third Employee Name::::::: = ";
	cin >> Emp3.EmployeeName;
	system("cls");
	cout << "\nPlease Enter Hours Worked:::::::: = ";
	cin >> Emp3.hours;
	system("cls");
	cout << "\nPlease Enter Hourly Wage   ::::::  = ";
	cin >> Emp3.wage;
	system("cls");
	cout << endl;


	/// Calculations   (( Formatting the calculations for Employees 1-3

	Emp1.ImplementCalculations(Emp1.EmployeeName, Emp1.hours, Emp1.wage);
	Emp2.ImplementCalculations(Emp2.EmployeeName, Emp2.hours, Emp2.wage);
	Emp3.ImplementCalculations(Emp3.EmployeeName, Emp3.hours, Emp3.wage);

// Return

	return 0;

} 


void EmpClass::ImplementCalculations (string EmployeeName, int hours, double wage)

{

// Overtime Place Holders (0)
ot_Hour=0;
ot_Pay=0;
ot_Xtra=0;



if (hours > 40)  // Determine If Overtime Required
	{		
// If hours > 40    Calculate Overtime Pay
	basepay = 40 * wage;
	ot_Hour = hours - 40;
	ot_Pay = wage * 1.5;
	ot_Xtra = ot_Hour * ot_Pay;
	indSal = ot_Xtra + basepay;


DisplayEMPinfo();

	}	
	else

	basepay = 40 * wage;
	indSal = ot_Xtra + basepay;



	
	DisplayEMPinfo();
} 


void EmpClass::DisplayEMPinfo () {

	// Display Results  ( Formatted as requested in syllabus )

	cout << "Employee Name ......................... " << setw(15) << EmployeeName <<endl;
	cout << "Base Pay .............................. " << setw(15) << basepay << endl;
	cout << "Hours in Overtime ..................... " << setw(15) << ot_Hour << endl;
	cout << "Overtime Pay Amount ................... " << setw(15) << ot_Xtra << endl;
	cout << "Total Pay ............................. " << setw(15) << indSal << endl << endl;
}



void EmpClass::Calcs (EmpClass Emp1, EmpClass  Emp2, EmpClass Emp3)
{
	TotHours = Emp1.hours + Emp2.hours + Emp3.hours;
	TotSal = Emp1.indSal + Emp2.indSal + Emp3.indSal;
	TotalOThour = Emp1.ot_Hour + Emp2.ot_Hour + Emp3.ot_Hour;

	// Display Totals ( Formatted as requested in syllabus )

	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	cout << "%%%%%%%%%%%%%%%% EMPLOYEE SUMMARY DATA %%%%%%%%%%%%%%%%" << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	cout << "%%%% Total Employee Salaries ........ = " << setw(15) << TotSal << endl;
	cout << "%%%% Total Employee Hours ........... = " << setw(15) <<TotHours << endl;
	cout << "%%%% Total Overtime Hours ........... = " << setw(15) <<TotalOThour << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;
	for(int i=0; i<55; i++)
		cout << "%";
	cout << endl;



	system("pause"); // System Pause To View During Debugging / Editing

	}
Last edited on
You forgot to put braces around lines 161-167.
Topic archived. No new replies allowed.