I have successfully loaded my text file into an array. However, when I use a function to search the array for a certain element and then echo another one, it doesn't seem to display anything at all.
I feel that the problem is that the "currentIndex" in my int loadData function is not referenced in my shopping function.
For anyone willing to assist me, here is the code in question:
You have a global variable currentIndex at line 16.
In addition, inside function loadData () there is a local variable with the same name.
This local variable "hides" the global one within the function, so it is the local version (defined on line 42) which is used.
When that function terminates, the local variables no longer exist.
Thus later on in function shopping() reference is made to currentIndex. This time it is the global version which is used. Since this is the very first time that this particular variable has been referred to at all, it still has the value which it had at the start of the program.
The "quick and dirty" solution is to delete the new definition at line 42.
It is still necessary to initialise it to zero. You could do that where the variable is first defined.
A better solution is not to use global variables, but instead to pass this variable as a parameter to any function which needs to use it. That would mean passing by reference to function loadData() in order that the value can be updated and retained after the function ends.