understaning and passing arguments

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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <string>

using namespace std;

void buy_items(); // the missing part of the code is here, within the parenthesis right?

int main()
{   
    
    const int 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();    //oh and also missing here too
    
    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;
     

     
}




Last edited on
You have declared
 
string player_inventory[MAX_ITEMS];

in main() so the scope of this array is inside the main only.
If you want to use it in some other function then you need to pass to the function. First declare
1
2
3
4
5
void buy_items(string arr[]){
// do something withe array

}

then in the main call the function by passing the array as the argument
 
buy_items( player_inventory);

Topic archived. No new replies allowed.