hello i am very confused on how to properly pass an array through functions with a struct without using a pointer. we are only supposed to use chapters 1-8 which does not include pointers.. here is my code if anyone has any suggestions or links to help out thank you!
const int MAX_DATA = 10000;
struct Inventory
{
double sku;
double count;
double cost;
string title;
};
void addMovie(Inventory data[], double count);
void allInfo(Inventory data[], double count);
int main ()
{
Inventory data[MAX_DATA];
int choice;
int i = 0;
double count = 0;
return 0;
}
void addMovie(Inventory data[], double count)
{
int i = 0;
cout << "Please enter the name of the movie you wish to add " << endl;
cin >> data[i].title;
cin.ignore();
cout << "Please enter the SKU " << endl;
cin >> data[i].sku;
cout << "You have successfully added " << data[i].title << " : " << data[i].sku << endl;
i++;
count++;
}
void allInfo(Inventory all[], double count)
{
for (int i = 0; i < count; i++)
{
cout << "Title: " << all[i].title << endl;
cout << "SKU: " << all[i].sku << endl;
i++;
}
}
For a fixed size array, something like this should work, passing the array by reference:
Note that this gives you will have to make sure not to go beyond the bounds of the array.
It would be way easier to use an
std::vector<Inventory>, in which case you don’t have to worry about the dimensions or the strange passign array by reference syntax: