Retaining struct data from a function

If I have a struct with an array in it included in my main and I use a function to read data into that array how do I retain that data after I leave the function and return to the main?

Is this a case where I have to make the function string instead of void and return the array?

If it is, do I have to move the array from my struct and declare it elsewhere?

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
 #define MAX_BOOKS 500

 struct bookType
	{
	public:
	 string headings[15];
	 };

int main()
{
 ifstream infile;
 bookType MyBooks;
 void getHeadings(istream& infile, string headings[]);
 infile.open("books.txt");

 getHeadings( infile, MyBooks.headings);

 cout << heading[4]; // <----- This is what I am trying to do.
 
 system("pause");
 return 0;
}

 void getHeadings(istream& infile,string  headings[])
 {
	for (int i = 0; i < 10; i++)
	{
		infile >> headings[i];
		 cout << headings[i] << endl;
	}
 }
Last edited on
Arrays are passed as pointers, so you will be modifying original array. However:
Line 18: You should do cout << MyBooks.heading[4];
Line 13: This is one of the ugliest things C++ can do: function declarations inside functions.
Either move it before main() as forward declaration, or delete it and move getHeadings definition before main().
Topic archived. No new replies allowed.