Results 1 to 2 of 2

Thread: array question

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Angry

    does any one know how i might go about doing this.
    it's a simple tictactoe game but when the game board
    function is called the values seem to be passed
    to the first element of the array. so if this is
    my game board -> _|_|_ only the first space on the
    _|_|_
    | |
    game board is always updated.

    int main(int argc, char* argv[])
    {
    cout << "Player 1 enter your cordinates:
    cin >> tictactoe[x1][y1];
    gameboard();

    return 0;
    }

    void gameboard()
    {



    cout << endl;
    cout << setw(5) << tictactoe[0][0] << setw(4) << tictactoe[0][1] << setw(4) << tictactoe[0][2] << endl;
    }




  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    When you refer to the array tictactoe[] in the function gameboard(), the array is not in scope. If you want to be able to access the array outside of the function it was declared in, you must declare it at a higher-leve scope (in this case global scope) or pass the array as an argument.

    Try:

    Code:
    int main(int argc, char* argv[]) 
    {  char* tictactoe[3][3]
       int x1, y1
       cout << "Player 1 enter your cordinates: 
       cin >> x1 >> y1; 
       gameboard(tictactoe[x1][y1]); 
    
       return 0; 
    } 
    
    void gameboard(char* tictactoe[3][3]) 
    {  cout << endl; 
       cout << setw(5) << tictactoe[0][0] 
            << setw(4) << tictactoe[0][1] 
            << setw(4) << tictactoe[0][2] 
            << endl; 
    }
    or something along those lines.

    I'm not quite sure how that code works with the rest of your program. If that's your whole program then maybe youought to rethink the structure of it.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width