iv taken all your feedback and corrected on my mistakes(btw i forgot bout using the for loop to show my array before lol) but despite that im still havn problems. The below is my code..
#include <iostream>
#include <string>
usingnamespace std;
void buy_items(string, int);
int main()
{
constint MAX_ITEMS = 10;
string player_inventory[MAX_ITEMS];
int item = 0;
player_inventory[item++] = "shoe";
for(int i = 0; i < 0; i++)
{
cout << player_inventory[i];
}
cout << "\n\nnow show the same array in a different function using arguments";
buy_items(string, int); //My dev C++ compiler tell me this about this particular line....25 expected primary-expression before ',' token
//lol okay what primary expression??
//btw iv already tried string player_inventory[], int item in all the declerations
int pause;
cin >> pause;
}
void buy_items(string player_inventory[], int item)
{
for(int i = 0; i < 0; i++)
{
cout << player_inventory[i];
}
cout << "add extra item to the same array from main() and show it in this function";
player_inventory[item++] = "hat";
for(int i = 0; i < 0; i++)
{
cout << player_inventory[i];
}
}
When you put the function like that in main, the compiler thinks you are trying to call the function; it looks like you just copied and pasted the prototype though. You have to pass the parameters like this:
buy_items(player_inventory, item);
Sorry if my explanation is not that clear.
EDIT: Also I noticed that the function is just making local copies of the variables you pass it. So when you add "hat" to their inventory, you are only adding it to that specific function's player_inventory and when you return from that function you lose that change.
Not to mention that the function prototype declares a function that expects a string, not an array of strings, so by passing player_inventory, you'll still get a compiler error.
#include <iostream>
#include <string>
usingnamespace std;
void buy_items(string player_inventory[], int item);//@@changed here
int main()
{
constint MAX_ITEMS = 10;
string player_inventory[MAX_ITEMS];
int item = 0;
//player_inventory[item++] = "shoe";
for(int i = 0; i <MAX_ITEMS; i++)
{
player_inventory[i]=""; //@@changed here, just initializing it with empty ""
}
cout << "\n\nnow show the same array in a different function using arguments";
buy_items(player_inventory,item);//@@changed here, passing your array an its current size=0
// call another time
item=5; // already filled up 5 times so we need to fill from 6 to 9 poisitions
buy_items(player_inventory,item);//@@changed here, passing your array an its current size=0
int pause;
cin >> pause;
return 0; // @@addded
}
void buy_items(string player_inventory[], int item)
{
cout << "Printing Before adding \n";
for(int i = 0; i < item; i++)//@ chaged the condition
{
cout << "At position " << i << " " << player_inventory[i] <<"\n";
}
cout << "adding 5 items \n";
player_inventory[item++] = "hat";
player_inventory[item++] = "bat";
player_inventory[item++] = "mat";
player_inventory[item++] = "rat";
player_inventory[item++] = "sat";
cout << "Printing Before adding \n";
for(i = 0; i <item; i++)
{
cout << "At position " << i << " " << player_inventory[i] <<"\n";
}
}