functions

i'm just starting to learn about functions and i can't figure out what i did wrong here... help?

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
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

//Function Prototypes
void numEmploy();
void numAbsent(int);
void Avg(double, double);

//Main
int main()
{
	numEmploy();
	numAbsent();
	average = Avg();
	cout << "The average number of days a company's employees are absent is " << average << endl;
	getch();
	return 0;
}

//Function Declarations

//numEmploy function
void numEmploy()
{
	cout << "How many people are employed at the company?\n";
	cin >> num;

	if (num < 1)
	{
		cout << "Please enter a number greater than 0.\n";
		cin >> num;
	}
	return num;
}

//numAbsent function
void numAbsent(int)
{
	int total = 0;
	int abs;

	while (num)
	{	
		total += abs;
		num++;
		cout << "Enter the number of days employee " << num << "missed: ";
		cin >> abs;
	}
	return total;
}

//avg function
void Avg(num, absent)
{
	(num + absent) / num = total;
	return total;
}
A void function does not return anything......
example: If you want a function(say avg()) to return a value of type int declare it as int avg()...
i tried that and it gave me the same or similar error messages
You are declaring your functions to take parameters but you aren't ever passing any.

I think you just need to look at the tutorial again and see what they did:
http://www.cplusplus.com/doc/tutorial/functions/

Btw, I would suggest not using conio.h/getch(), since they are nonstandard.
i have not gone throug the code properly...htere are a lot of mistakes in it......
you have written
average = Avg( );

but you have not defined what average is.....also the function Avg requires two arguments(you did not pass any arguments to it).....

I think uou have a misconception that whrn you call a function(say numEmploy)...the variables defined in the function become global.....That is not right...

this should work

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

//Function Prototypes
int numEmploy();
float Avg(int, int);
int numAbsent(int);

//Main
int main(){
	int num=numEmploy();
	//numAbsent(num);
	floathj average = Avg(num,numAbsent(num));
	cout << "The average number of days a company's employees are absent is " << average << endl;
	getch();
	return 0;
}

//Function Declarations

//numEmploy function
int numEmploy()
{
	int num;
    cout << "How many people are employed at the company?\n";
	cin >> num;

	if (num < 1)
	{
		cout << "Please enter a number greater than 0.\n";
		cin >> num;
	}
	return num;
}

//numAbsent function
int numAbsent(int num)
{
	int total = 0;
	int abs,tot=0;

	while (tot<num)
	{	
		tot++;
		cout << "Enter the number of days employee " << tot << "missed: ";
		cin >> abs;
		total += abs;
	}
	return total;
}

//avg function
float Avg(int num,int absent){
    float total;
    total= (1.0* absent )/ num;
	return total;
}


any further explanation is not good.....
i hope you understand what ur mistake is....if not better refer the theory again


Last edited on
thanks for the help!
Topic archived. No new replies allowed.