title: Game of Life program, help needed with user input
body: <p>I have a basic 'Game of Life' program in C++ and I need to alter to accept user input for both the size of the grid and the parameters for how many neighbors will make a cell alive or dead. For right now, I am focusing on the user input for the size of the grid, but I'm having problems with 'undeclared identifiers' for the maxrow and maxcol variables. Here is the code, and at the bottom is the function I made to accept user input. The program works as is, but when I try and implement the user input function it gives me all those errors. I know I have to delete the constant ints inside the header file, but I think my errors are coming from certain functions not understanding what maxrow and maxcol are, for example the 'instructions' function. How can I implement it in a way where the variables will be recognized throughout the entire program? Thank you for any help.</p>
  
  <p><strong>life.h</strong></p>
  
  <pre><code>#ifndef LIFE_H
  #define LIFE_H
  
  const int maxrow = 20, maxcol = 60; 
  
  class Life
  {
    public:
      Life(void);
      ~Life(void);
      void initialize();
      void print();
      void update();
      int getNeighbor_count(int, int);
      void instructions();
      bool user_says_yes();
  
    private:
      int grid[maxrow+2][maxcol+2];   // allows for two extra rows and columns
      int neighbor_count(int row, int col);
  };
  
  #endif
  </code></pre>
  
  <p><strong>life.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt; 
  #include "life.h"
  using namespace std;
  
  
  Life::Life()
  {
  }
  
  
  Life::~Life(void)
  {
  }
  
  
  int Life::neighbor_count(int row, int col)
  /* Pre: The Life object contains a configuration, and the coordinates row and col define a cell inside its hedge.
  Returns: The number of living neighbors of the specified */
  {
      int i, j;
      int count = 0;
      for(i = row-1; i &lt;= row+1; i++)
        for(j = col-1; j &lt;= col+1; j++)
         count += grid[i][j]; // Increase if neighbor is alive.
      count -= grid[row][col]; // Reduce count, cell is not its own neighbor.
      return count;
  
  }
  
  void Life::update( )
  /*Pre: The Life object contains a configuration.
    Post: The Life object contains the next generation of configuration.*/
  {
     int row, col;
     int new_grid[maxrow + 2][maxcol + 2];
     for(row = 1; row &lt;= maxrow; row++)
        for(col = 1; col &lt;= maxcol; col++)
           switch (neighbor_count(row, col)) {
             case 2: // Status stays the same.
               new_grid[row][col] = grid[row][col];             
               break;
             case 3: // Cell is now alive.
               new_grid[row][col] = 1;
               break;
             default: // Cell is now dead
               new_grid[row][col] = 0;
           }
    // Copy new configuration back to original 
    for(row = 1; row &lt;= maxrow; row++)
        for(col = 1; col &lt;= maxcol; col++)
            grid[row][col] = new_grid[row][col];
  }
  
  
  void Life::initialize( )
  /* Pre: None.
     Post: The Life object contains a configuration specified by the user.*/
  {
     int row, col;
     for(row = 0; row &lt;= maxrow+1; row++)
        for(col = 0; col &lt;= maxcol+1; col++)
      grid[row][col] = 0;
     cout &lt;&lt;"List the coordinates for living cells." &lt;&lt; endl;
     cout &lt;&lt; "Terminate the list with the the special pair -1 -1"&lt;&lt; endl;
     cin &gt;&gt; row &gt;&gt; col;
     while (row != -1 || col != -1) 
     {
        if(row &gt;= 1 &amp;&amp; row &lt;= maxrow)
           if(col &gt;= 1 &amp;&amp; col &lt;= maxcol)
               grid[row][col] = 1;
           else
               cout &lt;&lt; "Column " &lt;&lt; col &lt;&lt; " is out of range." &lt;&lt; endl;
        else
               cout &lt;&lt; "Row " &lt;&lt; row &lt;&lt; " is out of range." &lt;&lt; endl;
        cin &gt;&gt; row &gt;&gt; col;
     }
  }
  
  void Life::print( )
  /*  Pre: The Life object contains a configuration.
      Post: The configuration is written for the user.
  */
  {
     int row, col;
     cout &lt;&lt; "\nThe current Life configurations is: "&lt;&lt; endl;
     for(row = 1; row &lt;= maxrow; row++) {
        for(col = 1; col &lt;= maxcol; col++)
           if(grid[row][col] == 1) cout &lt;&lt; '*';
           else cout &lt;&lt; ' ';
        cout &lt;&lt; endl;
     }
     cout &lt;&lt; endl;
  }   
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt; 
  #include "life.h"
  using namespace std;
  
  void instructions();
  bool user_says_yes();
  
  void Life::instructions( )
  /* Pre: None.
     Post: Instructions for using the Life program have been printed.*/
  {
     cout &lt;&lt; "Welcome to Conway's game of Life." &lt;&lt; endl;
     cout &lt;&lt; "This game uses a grid of size "
          &lt;&lt; maxrow &lt;&lt; " by " &lt;&lt; maxcol &lt;&lt; " in which" &lt;&lt; endl;
     cout &lt;&lt; "each cell can either be occupied by an organism or not." &lt;&lt; endl;
     cout &lt;&lt; "The occupied cells change from generation to generation" &lt;&lt; endl;
     cout &lt;&lt; "according to the number of neighboring cells ";
     cout &lt;&lt; "which are alive." &lt;&lt; endl;
  }
  
  bool Life::user_says_yes()
  {
     int c;
     bool initial_response = true;
     do { // Loop until an appropriate input is received.
        if(initial_response)
           cout &lt;&lt; "Would you like to see the next generation (y,n)? " &lt;&lt; flush;
        else
           cout &lt;&lt; "Respond with either y or n: " &lt;&lt; flush;
        do { // Ignore white space.
           c = cin.get( );
        } while (c == '\n' || c == ' ' || c == '\t');
        initial_response = false;
    } while (c != 'y' &amp;&amp; c != 'Y' &amp;&amp; c != 'n' &amp;&amp; c != 'N');
   return (c == 'y' || c == 'Y');
  }
  
  int Life::getNeighbor_count(int row, int col)
  {
      return neighbor_count(row, col);
  }
  
  
  int main()// driver for neighbor_count( )
  /* Pre: None.
     Post: Verifies that the method neighbor_count( )returns the correct values.
     Uses: The class Life and its method initialize( ).
  */
  {
      Life configuration;
      configuration.instructions();
      configuration.initialize();
     do{
     for(int row = 1; row &lt;= maxrow; row++){
        for(int col = 1; col &lt;= maxrow; col++)
           cout &lt;&lt; configuration.getNeighbor_count(row,col) &lt;&lt; " ";
           cout &lt;&lt; endl;
     }
     configuration.print();
     configuration.update();
     }while(configuration.user_says_yes());
  
  }
  </code></pre>
  
  <p><strong>user input function for max column and max row</strong></p>
  
  <pre><code>void getGridParameters(int&amp; maxrow, int&amp; maxcol)
  {
      cout &lt;&lt; "Enter number of rows for the grid: ";
      cin &gt;&gt; maxrow;
      cout &lt;&lt; "Enter number of columns for the grid ";
      cin &gt;&gt; maxcol;
  }
  </code></pre>