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.
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.
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();
}
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).
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.