okay, so iv tried to pass the argument, of allowing my array from main to work in my function buy_items()...the only thing is i must now enter something in, where i have stated "missing part of code here" iv tried a few things but they dnt work, So hopefully someone here knows?
#include <iostream>
#include <string>
usingnamespace std;
void buy_items(player_inventory, item); //missing part of code here. Within the parenthesis the arguments seems to be invalid and my compiler keeps lettn me know this. Iv obvs done somet wrong, anyone care to enlighten me?
int main()
{
constint MAX_ITEMS = 10;
string player_inventory[MAX_ITEMS];
int item;
player_inventory[item++] = "shoe";
cout << player_inventory;
cout << "\n\nnow show the same array in a different function using arguments";
buy_items(player_inventory, item);
int pause;
cin >> pause;
}
void buy_items(string player_inventory[], int item)
{
cout << player_inventory;
cout << "add extra item to the same array from main() and show it in this function";
player_inventory[item++] = "hat";
cout << player_inventory;
}
Copy the function header in the definition to the declaration?
cout << player_inventory should print a memory address, by the way.
And you must initialize item before doing item++.
You forgot to put the type of the variables you are using.
In the declaration you either have the type of the variables and the names, or just the type, you can not have just the names:
For example:
1 2 3
myFunction(int num1, double num2); //legal
myFunction(int , double);//Legal
myFunction(num1, num2) illegal... Your program doesn't know what the variables are...
Also you use the variable "item" in your main without initialization.
void buy_items(string inventory[] , int items); // this is a declaration, this means this function has two arguments, first arguments is an array of strings second argument is an integer. It has no return (it returns a void)
Now at Line 29 we will define this function
void buy_items(string inventory[], int items)
{
// The argument int items represents the size of the array
// To display the contents of inventory we need to go through each
//element in the array
for(int i=0; i<items; i++){
cout << inventory[i] <<"\n";
}
cout << "add extra item to the array \n";
inventory[item++] = "hat";
// To display all items again
for(int i=0; i<items; i++){
cout << inventory[i] <<"\n";
}
// Remember int items should never exceed the max_size of array=10
}