What's wrong?

I can't seem to find out why this won't compile. This is my objective:

Write a program that calculate the average number of days a company's employees are absent. The program should include the following functions:

- A function called by main that asks the user for the number of employees in the company. This value should be returned as an int. (The function accepts no arguments)

- A function called by main that accepts one argument: the number of employees in the company. The function should ask the user to enter the number of days each employee missed during the past year. The total of these days should be returned as an int.

- A function called by main that takes two arguments: the number of employees in the company and the total number of days absent for all employees during the year. The function should return , as a double, the average number of days absent. (This function does not perform screen output and does not ask the user for input)

NOTE: Do not accept a number less than 1 for the number of employees. Do not accept a negative number for the days an employee missed.

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
#include<iostream>
using namespace std;

int num_emp_f();
int num_days_f(int num_emp);
double avg_abs_f(int num_emp, int num_days);
int main()
{
	int num_emp = 0;
	int num_days = 0;
	double avg_abs = 0.0;

	cout << "Average employee absent days last year: " << avg_abs;

	system("pause");
	return 0;
}
int num_emp_f()
{
	int num_emp;

	cout << "Enter # of Employees";
	cin >> num_emp;

	while(num_emp < 1)
	{
		cout << "INVALID, # of employees must be > 1.";
		cout << "Enter # of Employees";
		cin >> num_emp;
	}	

	cout << "# of employees = " << num_emp;
}
int num_days_f(int e)
{
	int num_days;

	for(int e = 0; e = num_days; e++)
	{
		cout << "Enter # of Days each employee missed last year";
		cin >> num_days;
	}
	
	while(num_days < 0)
	{
		cout << "INVALID, # of days missed must be atleast 0.";
		cout << "Enter # of Days each employee missed last year";
		cin >> num_days;
	}

	cout << "# of Days missed = " << num_days;
}
double avg_abs_f(int e, int d)
{
	int num_emp;
	int num_days;
	double avg_abs;

	avg_abs = (num_emp*365)/num_days;

}
Last edited on
Well, posting the error message would help.
Also, this isn't Python, the compiler doesn't care about whitespace.
white space is just my personal preference, i find it easier to read.

heres the error:
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(35): error C4716: 'num_emp' : must return a value
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(52): error C4716: 'num_days' : must return a value
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(40): warning C4700: uninitialized local variable 'num_days' used
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(61): error C4716: 'avg_abs' : must return a value
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(59): warning C4700: uninitialized local variable 'num_emp' used
1>c:\users\derek\documents\visual studio 2010\projects\assignment06\assignment06\assignment06.cpp(59): warning C4700: uninitialized local variable 'num_days' used
white space is just my personal preference, i find it easier to read.

My point was you were missing braces, but I see you fixed it now.

The compiler errors are due to some variable names having the same name as the enclosing function.
This also makes no sense, I suggest having a look at for loops again:
for(int e = 0; e = num_days; e++)
The comparison operator is ==, by the way.
yeah i saw the braces problem, i'm taking a python class right now so i sometimes get the two mixed up.

i changed the function names to slightly differ from the variable names, but i'm still getting an error. I think its due to the for loop you said didn't make sense. i'm new to for loops, and don't entirely understand what you mean by comparison operator. isn't == used in python to show equality, and c++ it's just a single =?
All of your functions return a value, but you aren't returning anything from them (you have no return statement anywhere.

Also, you keep creating local variables (like in avg_abs you make num_emp etc), but you don't set them to anything, and then you try to use them which is undefined.
No, it's == in C++ as well. = is the assignment operator.
Also, the middle part of the for loop is the condition that needs to be fulfilled in order for the loop to continue.
well the local variables are being set in each function by the user, correct? With the input statements. At least that's what i'm trying to do.

as for not returning anything, i've added a few couts. i'll post new code.
Returning a value is done with the keyword return.
And local variables are local to the scope they're declared in. You'll have to pass the variables as parameters if you want to use them in multiple functions.
I got it, thanks so much for the help!
I have made complete program by debuging your program.If any problem occurs then you can asks question.You can also visit my blog www.codeincodeblock.blogspot.com for programming tips and tricks and project source code.
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
#include<iostream>
#include<windows.h>
using namespace std;

int num_emp();
int num_days(int);
double avg_abs(int,int);
int main()
{
	int num_emp_data = 0;
	int num_days_data = 0;
	double avg_abs_data = 0.0;
	num_emp_data=num_emp();
	num_days_data=num_days(num_emp_data);
	avg_abs_data=avg_abs(num_emp_data,num_days_data);
    cout << "Average employee absent days last year: "<<endl<< avg_abs_data;
    cout<<endl;
	//system("pause");
	return 0;
}
int num_emp()
{
	int num_emp1;

	cout << "Enter # of Employees"<<endl;
	cin >> num_emp1;

	while(num_emp1 < 1)
	{
		cout << "INVALID, # of employees must be > 1.";
		cout << "Enter # of Employees";
		cin >> num_emp1;
	}
	return num_emp1;
}
int num_days(int e)
{
	int num_emp1=e;
	int num_days1;
	int total_days=0;

	for(int i = 0; i < num_emp1; i++)
	{
		cout << "Enter no. of Days "<<i+1<<" employee missed last year"<<endl;
		cin >> num_days1;
		total_days+=num_days1;
	}

	while(num_emp1 < 0)
	{
		cout << "INVALID, # of days missed must be atleast 0.";
		cout << "Enter # of Days each employee missed last year";
		cin >> num_days1;
	}
	return total_days;
}
double avg_abs(int e, int d)
{
	int num_emp2=e;
	int num_days2=d;
	double avg_abs1;

	avg_abs1 = (num_emp2*365)/num_days2;
	return avg_abs1;

}
You shouldn't post full solutions http://www.cplusplus.com/forum/articles/31015/

1
2
3
4
double avg_abs(int e, int d) //parameters passed by copy
{
	int num_emp2=e; //making a copy of the parameters ¿why?
	int num_days2=d;
Also ¿why did you #include<windows.h> for?
#include<windows.h> is for system("pause"); when we compile in gcc compiler.
system is defined in cstdlib. But you shouldn't use that http://www.cplusplus.com/forum/articles/11153/
Topic archived. No new replies allowed.