still problems >.>

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..

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
49
50
51
52
53
54
#include <iostream>
#include <string>

using namespace std;

void buy_items(string, int);

int main()
{   
    
    const int 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];
     }
     

     
}
Last edited on
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.
Last edited on
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.
Here is a working program, please see my comments and go through the code
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>

using namespace std;

void buy_items(string player_inventory[], int item);//@@changed here

int main()
{   
    
    const int 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";
     }
     

     
}
Last edited on
Ok. Some feed back:

Line 55: Error, it should be int i = 0 not i = 0;. i has not been previously declared and causes compiler errors.

Line 39: you can use:
1
2
for(int i = 0; i < item; i++)//@ chaged the condition
  cout << "At position " << i << " " << player_inventory[i] <<"\n";

instead of using { /* and */ }. Same with lines 55-58.

Line 12: You can use string player_inventory[MAX_ITEMS] = { "" } instead of the loop at lines 17-20.
Topic archived. No new replies allowed.