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
#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!
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.