Arrays and Functions

Hi,

I am trying to make a function that takes an array as a parameter. However, I can't seem to get it to work. Could someone help me please.

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
68
//Author:       *******
//Date: 	03/11/2013
//Title:        Calculating Employees Gross Pay
//Description:  Asks user to input employee id, hourly rate & number of hours and then outputs each employees gross pay

#include <iostream>
#include <cstdlib>

using namespace std;

void get_ID (int &employee_ID);//function prototype of type int that take an int as its argument
void populate_array (int employee_ID, int terminator, int &count, int &number_of_employees, int &ID[5]);

int main ()
{
	const int terminator = 9999;//declaring constant variable of type int which is used in the test condition for the do-while loop
    const int size_of_array = 5;//declaring constant variable of type int which is used to set size of array
    
	int count = 0;//declaring variable of type int which is used to identify the element of each array
	int number_of_employees;//variable of type int which is used to make sure size of each array is not exceeded
	int employee_ID;//declaring variable of type int that holds each employee ID

	int ID [size_of_array];//declaring an array of type int that has maximum of 50 elements and holds each employee ID
	
	do
	{
		cout << "\nNew Loop" << endl;
		cout << "=====================" << endl;
		cout << "Value of count start of loop: " << count << endl;
		get_ID (employee_ID);
		populate_array (employee_ID, terminator, count, number_of_employees, int ID[5]);
		/*if (employee_ID <= terminator)
		{
			ID [count] = employee_ID;
			count ++;//increments count by +1 each time loop executed
        	number_of_employees = count;//setting value of variable "number_of_employees"
		}
        else
            break;*/
        cout << "\nValue of count end of loop: " << count << endl;
        cout << "Value of number of employees end of loop: " << count << endl;
	}while (employee_ID <= terminator && count < size_of_array);
	cout << "Left loop - employee_ID is: " << employee_ID << endl;
	//system ("CLS");
	int x;
	cin >> x;
	return 0;
}

//function that gets user to enter employee ID and passes out the value
void get_ID (int &employee_ID)
{
	cout << "\nPlease enter Employee's ID (above 9999 to terminate): ";//prompts user to enter employee's ID or terminate program
	cin >> employee_ID;//reads inputted value and initialises variable employee_ID
}

void populate_array (int employee_ID, int terminator, int &count, int &number_of_employees, int &ID[5])
{
    if (employee_ID <= terminator)
		{
			ID [count] = employee_ID;
			count ++;//increments count by +1 each time loop executed
        	number_of_employees = count;//setting value of variable "number_of_employees"
		}
        else
            break;
}
http://www.cplusplus.com/doc/tutorial/arrays/

Prototype is incorrect
void populate_array(int employee_ID, int terminator, int &count, int &number_of_employees, int &ID[5]);

You can put name is there if you like but they just get ignored anyway but the main issue there is this int &ID[5]

Just need:
void populate_array(int, int, int&, int&, int[]);

Just says we are going to pass in 2 ints 2 ints addresses and an int array.

Function call is incorrect
populate_array (employee_ID, terminator, count, number_of_employees, int ID[5]);

Arrays are passed by reference because array names are just const. pointers anyway. So we just pass in the array name.

populate_array(employee_ID, terminator, count, number_of_employees, ID);

Line 57 is incorrect
void populate_array (int employee_ID, int terminator, int &count, int &number_of_employees, int &ID[5])

Change to
void populate_array(int employee_ID, int terminator, int &count, int &number_of_employees, int ID[])

You have a few other issues as well, hope this helps.
Thanks Mobotus, I really appreciate you taking the time to look at my code and share your knowledge with me.

I will try and implement your suggestions and see how I get on.

***edit: got it working now thanks to your help. You are the best.***
Last edited on
Topic archived. No new replies allowed.