I am trying to make an employee program that takes in up to 100 max employees information then displays it back out nicely formatted, which I am not getting right and have been googling wtf I am doing wrong. Also I was wondering after inputting all the employee records how can I take and get an average of the companies age and income...if someone can please guide me in the right direction as I have been at this for 2 days really trying to learn and not sure what I am doing wrong. Thank you.
*Don't use system("PAUSE"); unless you are making the program for windows only.
So. What's going wrong? Well.. I don't like how you display the title too much, but it is fine. So what's causing the awkward tabs for the other outputs? Well. You don't have a setw() for "Employee Name" and the corresponding employeeName. Make it something sensible like 20 or something, then try your program again. Don't like everything being justified to the right? Then use the function 'left' when outputting.
Thank you for your prompt response back, I will be trying this momentarily.
What really is causing me to lose sleep though is after collecting all the data from various objects how can I calculate an average of age and salary? I am getting lost on how to step through the array of objects stored to determine such output.
int totalAge = 0;
int averageAge = 0;
for (int index = 0; index < NumberOfEmployees; index++)
{
totalAge += employees[index].employeeAge;
}
averageAge = totalAge/NumberOfEmployees;
You will have to keep track of how many employees are actually input.
Thank you for the guide in the right direction as your formatting worked great, but I am stuck again I have gone through my book and research for options, I understand that the object declared knows how many records are stored. I am not understanding how to define employees and NumberOfemployees. Pulling my hair out here really trying to learn, hopefully I can do this. :>
You did employees correctly if you want to limit the number of employees to 100.
This is fine. You would have to use dynamic memory if you want the user to decide.
But whichever way you go, keeping track of NumberOfEmployees is not very hard:
Yeah. You are still testing i against n. What is n? Well I'll tell you what it isn't. It isn't the number of employees.
This is why we use words that help us describe what a variable is.
If someone looks at your code and sees n, they will ask what the heck n is.
If someone sees numberOfEmployees, then they know what it is.
If you want to change the name of a variable, do a simple search for numberOfEmployees, you find all the variables you need to change. Do a search for 'n' guess what, you find a bajillion ns in your code. Same goes for 'i' which is why I use index.
I know you are right. I want to be able to understand this so bad and day in and day out goes by of me just reading and reading and same things over and over and I don't know what to say.
I wrote this code completely from scratch that is suppose to meet criteria of an assignment.
Then read a book. I read two C++ books in one day (and learned a lot). They aren't hard to read, and 500 pages isn't too long if you remember what you read and apply it to what you're reading.
I don't have any digital C++ books, but there are plenty of great tutorials online. Read them. Read forums where people argue about C++ stuff. The easiest way to get many different ways to do something is posting on a good C++ forum that such and such way is the best.
Read read read. Don't try until you read. There is no try. You must know before you do. Otherwise you are wasting precious time. Don't waste time reinventing the wheel. Waste time adding to the work of others to make a better wheel. But you need to know what they did first in order to improve it.
The problem is simple. data_analysis function is not part of the Employee class, correct? Therefore, it cannot access private members of Employee objects. employeeAge is a private member.
You never call display_stats function. Also. averageAge in that function does NOT exist. You declare averageAge in the data_analysis function. Which is fine. But the display_stats function does not have an averageAge. Read of 'scope' on google or something.
employees[i] you forgot to change this to employees[counter].
I made all the functions, member functions. Also made some updates, still not getting to being able to calculate, once I can figure this out, I am to calculate average income, but should be able to, once I get a solution for the age.
Ok. First off. You need to get something out of your head. Variables that you create in function1 CANNOT be used in function2. You need to pass the variables around some way. You can't just access them from one function to the next.
It goes with everything my friend. Functions don't change the way they work just cus they are inside a class. They all work the same. The ONLY difference is that member functions in a class, can access the member variables of their CLASS. That is it.
*But this is due to scope of course. Not something special that functions get.