Arrays as parameters help

I have tried to look at the http://www.cplusplus.com/doc/tutorial/arrays/ chapter Arrays as Parameters and although I think I understand the general point of it, I am still having trouble with my code.

I am trying to pass my array of data structures to a function, and get that function to display the Name, Acronym and Country of each of the array structures
e.g. airports[0].Name
airports[1].Name
etc



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 "coursework.h"											
#include "std_lib_facilities.h"
#include "DisplayMenu.h"
#include "Airport.h"

Airport airports[];

int main()
{
	
	Airport * airports = new Airport[4];
	airports[0].Name = "Heathrow";
	airports[0].Acronym = "LHR";
	airports[0].Country = "England";

	airports[1].Name = "Paris";
	airports[1].Acronym = "PR";
	airports[1].Country = "France";

	airports[2].Name = "Madrid";
	airports[2].Acronym = "MAD";
	airports[2].Country = "Spain";

	airports[3].Name = "Vegas";
	airports[3].Acronym = "LVS";
	airports[3].Country = "America";

	
	//cout << size << endl;                          //ignore this, this is just error checking
	//cout << airports[0].Name << endl;
	//DisplayMenu();
	//DisplayAirports();

	//int size;
	//size = sizeof( airports );
	//cout << size;		        			//error checking, this returns the value 4 
	
	//for( int i = 0; i < 4; i++)	                        //This displays the result I want but I want it within a function
	//{	
	//cout << "Name   : " << airports[i].Name << endl;	//Name   : Heathrow
	//cout << "Acronym: " << airports[i].Acronym << endl;	//Acronym: LHR
	//cout << "Country: " << airports[i].Country << endl;	//Country: England
	//cout << endl;				        	//etc
	//}																
	
	DisplayAirports(airports, 4);


	keep_window_open();
	return 0;
}

void DisplayAirports(int arg[], int length)
{
	for (int i = 0; i < length; i++)
	{
		cout << arg[i].Name << endl;
	}
}


I think the issue is with the (int arg[], portion, as that is the only error message that comes up now. However with returning both string and integer values I don't know what to declare the arg[] as?

It surely shouldn't be this difficult to display these should it? I want the function to simply go
1
2
3
4
//loop n = 1 
cout << airports[n].Name << endl;
//loop n = 2 
cout << airports[n].Name << endl;


I will think you an utter lad if you can help me out here because I am struggling with this minor problem! THANK YOU!
airports is not an array of int.
Airport
Its not int arg[] as arg[] isn't of type integer, it belongs to a class Airport, so it would be Airport arg[].

EDIT: I didn't know what Airport class looked like, so since classes and structures "work" similarly, I tried this: http://codepad.org/XiQwdxXZ
Last edited on
You should declare arg[] as Airport arg[], because this is the type of array you are trying to pass.
Last edited on
Thank you for your help guys! I was aware that int wasn't the correct type to use however when I looked it up I was trying to do DisplayAirports(airports arg[], int) instead of using the data structure not the instance of it.

You have been very helpful! Thank you very much!

Regards,
Topic archived. No new replies allowed.