average

C

C++Geek

I need to get this program to average the salaries. What am I doing
wrong?




//Program to read in employee data and calculate the average salaries
of the emplyees.


#include <iostream>
using namespace std;

const int n =10;


struct date
{
int month;
int day;
int year;
}calendar;

struct employee
{
char name[20];




date HireDate;
double salary;
}EmpInfo;

void employee_information(employee EmpInfo, date calendar);

double average(employee emp[], int n);

int main()

{
employee emp[n];
int actual_emp;


do
{
cout<<"Please enter the number of employees less than or = 10"<<endl;
cin>>actual_emp;
}

while

(actual_emp >= n);



for(int employee=0; employee < actual_emp; employee++)

employee_information(EmpInfo, calendar);
average(emp, n);

cout<<employee++<<endl;
average(emp, n);

return 0;

}

void employee_information(employee EmpInfo, date calendar)

{

cout<<"Please enter Employees name"<<endl;
cin>>EmpInfo.name;
cout<<"Please enter hire month"<<endl;
cin>>EmpInfo.HireDate.month;
cout<<"Please enter hire day"<<endl;
cin>>EmpInfo.HireDate.day;
cout<<"Please enter hire year"<<endl;
cin>>EmpInfo.HireDate.year;
cout<<"Please enter the emplyees salary"<<endl;
cin>>EmpInfo.salary;


cout<<"This employee was hired on "<<EmpInfo.HireDate.month<<" "
<<EmpInfo.HireDate.day<<" in the year
"<<EmpInfo.HireDate.year<<endl;
cout<<"His current salary is $"<<EmpInfo.salary<<endl;
}

double average(employee emp[], int n)

{
int sum =0, actual_emp;
sum = sum + EmpInfo.salary;

cout<<sum/actual_emp;

return 0 ;
}
 
A

Artie Gold

C++Geek said:
I need to get this program to average the salaries. What am I doing
wrong?

Well, for one thing, you're not asking a good question.
For one thing, you need to *tell* us what you expect and what's actually
happening.
//Program to read in employee data and calculate the average salaries
of the emplyees.


#include <iostream>
using namespace std;

const int n =10;


struct date
{
int month;
int day;
int year;
}calendar;

What is purpose of having *one* of these?
struct employee
{
char name[20];




date HireDate;
double salary;
}EmpInfo;
Similarly.
void employee_information(employee EmpInfo, date calendar);

`void' functions that take arguments by value tend to be suspect (unless
they only exist for their side effects; this will come up later).
double average(employee emp[], int n);

int main()

{
employee emp[n];
int actual_emp;


do
{
cout<<"Please enter the number of employees less than or = 10"<<endl;
cin>>actual_emp;

You really should have error checking here -- but it's early in the
semester.
}

while

(actual_emp >= n);



for(int employee=0; employee < actual_emp; employee++)

employee_information(EmpInfo, calendar);

Somehow, I don't the above function call accomplishes what you want. In
fact, all it does is `fill' a pair of local structures that are copies
of the global (uninitialized) ones -- and throws them away!
average(emp, n);
When have you put anything into `emp'?
cout<<employee++<<endl;
What is `employee' here? The loop variable above has gone out of scope..
average(emp, n);
....and, in any event, what is the purpose of taking same `average' (in
quotes because it's working on an uninitialized array?
return 0;

}

void employee_information(employee EmpInfo, date calendar)

{

cout<<"Please enter Employees name"<<endl;
cin>>EmpInfo.name;
cout<<"Please enter hire month"<<endl;
cin>>EmpInfo.HireDate.month;
cout<<"Please enter hire day"<<endl;
cin>>EmpInfo.HireDate.day;
cout<<"Please enter hire year"<<endl;
cin>>EmpInfo.HireDate.year;
cout<<"Please enter the emplyees salary"<<endl;
cin>>EmpInfo.salary;


cout<<"This employee was hired on "<<EmpInfo.HireDate.month<<" "
<<EmpInfo.HireDate.day<<" in the year
"<<EmpInfo.HireDate.year<<endl;
cout<<"His current salary is $"<<EmpInfo.salary<<endl;
}

double average(employee emp[], int n)

{
int sum =0, actual_emp;
sum = sum + EmpInfo.salary;

cout<<sum/actual_emp;

return 0 ;
}

Answer the above questions for yourself and you'll be well on the way to
getting this program written.

HTH,
--ag
 
I

I V

C++Geek said:
I need to get this program to average the salaries. What am I doing
wrong?

Generally, it's a good idea when posting a question to tell us what you
expect to happen, and what actually does happen. In this instance, we
can make some fairly obvious guesses, but sometimes although it is
obvious to you, it isn't obvious to us.

A couple of points which are not directed to your main problem:
int main()

{
employee emp[n];

which can be resized to hold as said:
int actual_emp;


do
{
cout<<"Please enter the number of employees less than or = 10"<<endl;

You should use 'n' here, rather than including the number in the
string.

cout << "Please enter the number of employees less than or
equal to " << n << endl;
cin>>actual_emp;
}

while

(actual_emp >= n);

Your array can hold n employees, so that condition could be actual_emp
n . Currently, the program will not let the user enter 10 employees, despite what you tell them above.
for(int employee=0; employee < actual_emp; employee++)

Calling your variable 'employee', when you have a struct of the same
name, is confusing.

Now we get into your main problem:
employee_information(EmpInfo, calendar);

Here, you're calling employee_information on the global variable
EmpInfo. You aren't doing anything to set the values of the employee
objects in your array, emp. What you need to do is pass each element of
your array to the employee_information function, like so:

employee_information(emp[employee], calendar);

There are further problems in your employee_information and average
functions, however (see below).
average(emp, n);

Note that this only gets called once, it is not part of the body of the
for loop, which is correct unless you want to print out a running
average as the user inputs information. Also, you are passing in the
size of your array, which maybe more than the number of employees
actually entered by the user. Instead, you want:

average(emp, actual_emp);
cout<<employee++<<endl;
average(emp, n);

Why are you calling average again here?
void employee_information(employee EmpInfo, date calendar)

Here, you are passing an employee object to the function. The object is
passed by value, which means that when you call the function, a new
employee object is created, a copy of the object that you pass to the
function. This newly created object is destroyed when the function
returns. Note that, although it is called EmpInfo, this newly created
object has nothing to do with the global EmpInfo object.

If you want to be able to access the changes you make to an object in a
function, you need to pass the object by reference:

void employee_information(employee& EmpInfo, date calendar)

(although it may be better style to have employee_information return an
employee object)
{

cout<<"Please enter Employees name"<<endl;
cin>>EmpInfo.name;
cout<<"Please enter hire month"<<endl;
cin>>EmpInfo.HireDate.month;
cout<<"Please enter hire day"<<endl;
cin>>EmpInfo.HireDate.day;
cout<<"Please enter hire year"<<endl;
cin>>EmpInfo.HireDate.year;
cout<<"Please enter the emplyees salary"<<endl;
cin>>EmpInfo.salary;


cout<<"This employee was hired on "<<EmpInfo.HireDate.month<<" "
<<EmpInfo.HireDate.day<<" in the year
"<<EmpInfo.HireDate.year<<endl;
cout<<"His current salary is $"<<EmpInfo.salary<<endl;
}

double average(employee emp[], int n)

{
int sum =0, actual_emp;
sum = sum + EmpInfo.salary;

Here, EmpInfo does refer to the global EmpInfo object. But that's only
_one_ object - you're ignoring the array of employees.

Further, actual_emp here does _not_ refer to the same actual_emp as in
main. Instead, it is a newly created int, with an undefined value.
You're ignoring the value, n, that was passed into the function.

What you need to do is loop through your array, adding up the saleries,
then, when you have a total, divide it by the total number of employees
passed to the function. I'll leave that as an exercise for you, but
feel free to ask further question if you can't get it to work.
return 0 ;

Instead of having your function print out the average, you could have
it return the average and print that out in main.

return static_cast<double>(sum) / n;

You need the cast because if you divide two integers, the answer is
rounded down, which would make the average wrong.
 
D

Daniel T.

"C++Geek said:
I need to get this program to average the salaries. What am I doing
wrong?

First let's make a functioning "average" function... Load the code below
in and run it. It will crash with some sort of message saying that "avg"
isn't the correct value. Put some code in the "average_salary_of"
function and keep running it until you can get the program to start
outputing "test x passed". Eventually, it should say "all tests pass",
by then your average function should work. If you have any problems,
come back and post the code you wrote.

struct date
{
int month;
int day;
int year;
};

struct employee
{
char name[20];
date HireDate;
double salary;
};

double average_salary_of( employee emp[], int n ) {
double result = 0.0;
// insert your code here.
return result;
}

int main() {
employee test_employees[5];
test_employees[0].salary = 3;
test_employees[1].salary = 3;
test_employees[2].salary = 3;

double avg = average_salary_of( test_employees, 3 );

assert( 2.9999 < avg && avg < 3.0001 );
cout << "test 1 works" << endl;

test_employees[0].salary = 4;
test_employees[1].salary = 4;
test_employees[2].salary = 4;

avg = average_salary_of( test_employees, 3 );

assert( 3.9999 < avg && avg < 4.0001 );
cout << "test 2 works" << endl;

test_employees[0].salary = 6;
test_employees[1].salary = 7;
test_employees[2].salary = 8;

avg = average_salary_of( test_employees, 3 );

assert( 6.9999 < avg && avg < 7.0001 );
cout << "test 3 works" << endl;

test_employees[0].salary = 7;
test_employees[1].salary = 8;
test_employees[2].salary = 6;

avg = average_salary_of( test_employees, 3 );

assert( 6.9999 < avg && avg < 7.0001 );
cout << "test 4 works" << endl;

cout << "all tests pass!" << endl;
}
//Program to read in employee data and calculate the average salaries
of the emplyees.


#include <iostream>
using namespace std;

const int n =10;


struct date
{
int month;
int day;
int year;
}calendar;

struct employee
{
char name[20];




date HireDate;
double salary;
}EmpInfo;

void employee_information(employee EmpInfo, date calendar);

double average(employee emp[], int n);

int main()

{
employee emp[n];
int actual_emp;


do
{
cout<<"Please enter the number of employees less than or = 10"<<endl;
cin>>actual_emp;
}

while

(actual_emp >= n);



for(int employee=0; employee < actual_emp; employee++)

employee_information(EmpInfo, calendar);
average(emp, n);

cout<<employee++<<endl;
average(emp, n);

return 0;

}

void employee_information(employee EmpInfo, date calendar)

{

cout<<"Please enter Employees name"<<endl;
cin>>EmpInfo.name;
cout<<"Please enter hire month"<<endl;
cin>>EmpInfo.HireDate.month;
cout<<"Please enter hire day"<<endl;
cin>>EmpInfo.HireDate.day;
cout<<"Please enter hire year"<<endl;
cin>>EmpInfo.HireDate.year;
cout<<"Please enter the emplyees salary"<<endl;
cin>>EmpInfo.salary;


cout<<"This employee was hired on "<<EmpInfo.HireDate.month<<" "
<<EmpInfo.HireDate.day<<" in the year
"<<EmpInfo.HireDate.year<<endl;
cout<<"His current salary is $"<<EmpInfo.salary<<endl;
}

double average(employee emp[], int n)

{
int sum =0, actual_emp;
sum = sum + EmpInfo.salary;

cout<<sum/actual_emp;

return 0 ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top