And you were using the "array" that was passed to the function like any other array. What happens when you pass an array to a function is that in the function you get a pointer to the first element.
You also need to pass the function how many elements are in the array or else you won't know how many there are and overshoot the end of the array.
So, the solution lies in using a for loop inside the function and using pointer arithmetic to initialize a new array of prodTypes one by one. Assuming you add a parameter to the function called arlen for the number of elements, you would do it like this:
1 2 3 4 5
prodType prod[arlen];
for(int i = 0; i < arlen; i++)
{
prod[i] = *(sales + i);
}
I'm sorry but your suggestion was a little hard to follow. I'm not exactly sure where I should add this code. I have tried it but it says arlen must have a constant value.
Can you post the file sales.txt. I guess the problem is more in loading the data from the file. After the file has loaded print it on the screen to see if you got the correct values.
1 2 3 4 5 6 7 8 9
if (inFile.is_open())
{
getMenuFromFile(inFile, sales);
}
else
{
cout << "Cannot open the input file." << endl;
}
inFile.close();
if the file can't be opend then there is no point to continue the program. Use exit(EXIT_FAILURE) to finish, also if it is not open when then close it?
@theturk1234,
You also need to pass the function how many elements are in the array or else you won't know how many there are and overshoot the end of the array.
It's not neccessary here since he uses the global const MAX_ITEMS.