C++ how to call Arrays out of separate function

My problem is creating a function or storing them in a function but calling upon those arrays to bring into main. So I created the function and started off with my smallest array to see if I can get it to work first before bringing anything else over. Sadly it did not work I brought array and its contents over to the function and tried to call up that specific array and bring into another function called PlayGame. Here is what I did in pictures. The code I have below is just to show I created the function stored one of my smaller arrays in it and tried but failed to call upon it in PlayGame. The program I felt was too big to put the whole thing in.


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


void instructions();

void InitializeVars();

void HeroInventory();

void PlayGame();

string WrongChoice[1]; 

int main()


void InitializeVars()
{

   
   
                
   WrongChoice[0] = "Sorry that is not a valid input please chose 1 or 2: "; 

}


     while( choice != 1 && choice != 2) 
      
      { 
      
      
        cout << InitializeVars(WrongChoice[0]) << endl; 
        
     
        cin >> choice; 
        
      } 
Last edited on
Show us how you tried to pass an array.

As an aside, arrays are a bad idea for beginners. You should consider using a vector instead. A vector works how you probably expect an array to work.
Lines 16-24: You can't embed a function inside main(). Move these lines to after line 12.

Lines 32: InitializeVars() is a void function. It doesn't return anything. You can't cout a void.
Also, InitializeVars doesn't take any arguments, so you can't pass WrongChoice to it.

You want to call InitializeVars() at the start of main. Then at line 32 you can:
1
2
3
  InitializeVars();
...
  cout << WrongChoice[0] << endl;


Last edited on
Topic archived. No new replies allowed.