I cant display a board for some reason I added a catch statemnt but there is something wrong with the if statment inside the for loop but i dont what is wrong thugh with the if else statement.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int size = 20;
void board_display(const bool [][size]);
void generations(bool [][size], bool [][size]);
int counting_neighbors(const bool [][size], int, int);
     bool grid[size][size];

int main()
{
    ifstream datafile;

    cout << "The input values from the file will begin the first generation." << endl << endl;


    // Initilize the grid to all dead cells                               
    //  bool grid[size][size];
     /*
    for (int i=0; i < size; i++)
        for (int j=0; j < size; j++)

            grid[i][j] = true;
    */
    int row, column;

    datafile.open("P3_input.dat");

    if (!datafile)
    {
        cout << "File not found" << endl;
        return 0;
    }
    else
      while (datafile)
	{
	  datafile >> row >> column;
	  
	  if ( row >= 0 && row < size && column >= 0 && column < size)  // checks for compatability     
            grid[row][column] = false;
	}
    
    //while ( row != -1);

    board_display(grid);

    bool temp[size][size];

    return 0;
}

void board_display(const bool World[][size])
{
        cout <<" 01234567890123456789\n";

    for (int i=0; i < 20; i++)
    {
       cout <<"test first loop\n";
        for (int j=0; j < 20; j++)
	  {
   cout <<" test second loop\n";
   if (grid[i][j] == true){
	      cout << "test 3" << endl;
                cout << "*";
   }
            else cout << " ";
	  }
        cout << endl;
    }
}

It looks like you never set grid[row][column] to true in the uncommented portion of the code.

Also, can you explain the expected output? Now (maybe related to my last statement), I see nothing output (after commenting the "test first loop" type statements. This leads me to believe that grid[j] is never true.

Also, please please please don't use global variables (grid here is global).

I have the program to work for some reason the board does not display the 19 column
and the board does not display anything after 6 row.

The files are attached. my output is given below.
NOTE: the row numbers do not appear in the actual output i just added easier to read the coordinates

01234567890123456789
0*....*...*.........
1......*.......*.*..
2....*..*...*..***.*
3..*..*...**......*.
4*..*.*...*.........
5..**.*.........***.
6.....**...**.**..**


 01234567890123456789
0...................
1.....**.......*.**.
2.....**...*...*.*..
3...*.**.***....***.
4.*.*.**..**......*.
5..**.*....*...*****
6....***.......**.**

 01234567890123456789
0...................
1.....**.........**.
2..........*...*....
3..*.....*..*...*.*.
4...*...**..*..*....
5..**.....**...*....
6...****.......*...*

 01234567890123456789
0...................
1...................
2...............*.*.
3.......*****..**...
4...*...**..*..**...
5..*..******..***...
6..****.............

 01234567890123456789
0...................
1...................
2........***...***..
3.......*.***.......
4...........**...*..
5..*..*...**..*.*...
6..****.***....*....

Edited 6 Years Ago by eagles39: n/a

Attachments
0   0
   0   5
   0   9
   0  19
   1   6
   1  14
   1  16
   2   4
   2   7
   2  11
   2  14
   2  15
   2  16
   2  18
   3   2
   3   5
   3   9
   3  10
   3  17
   3  19
   4   0
   4   3
   4   5
   4   9
   5   2
   5   3
   5   5
   5  15
   5  16
   5  17
   6   5
   6   6
   6  10
   6  11
   6  13
   6  14
   6  17
   6  18
   7   6
   7   8
   7  14
   7  16
   8   1
   8   5
   8   8
   8  10
   8  17
   9   3
   9   4
   9   6
   9   7
  10   0
  10   9
  10  19
  11   1
  11   2
  11   3
  11   5
  11   7
  11   8
  11  14
  11  17
  11  19
  12   4
  12   5
  12   6
  12   8
  13   0
  13   1
  13   7
  13  10
  13  11
  13  14
  13  15
  13  16
  13  17
  13  18
  14   2
  14  11
  14  13
  14  15
  14  17
  14  19
  15  11
  15  13
  15  18
  16   1
  16   2
  16   5
  16   8
  16  17
  17   5
  17   6
  18   6
  18  16
  18  18
  19   0
  19   3
  19   4
  19   7
#include <iostream>
#include <fstream>
using namespace std;

//The Board class of the game of life. The grid is a 2D array.

	int row, column;
	bool grid [20][20];

	int Board(int, int);
	void nextGeneration();
	void printBoard();
        ifstream datafile;


int Board(int a, int b) {
	column = a;
	row = b;
	
	//Presets of alive cells. 
/*  grid[0][1] = true;
	grid[1][2] = true;
	grid[2][0] = true;
	grid[2][1] = true;
	grid[2][2] = true;

*/

   datafile.open("board.txt");
 if (!datafile)
    {
        cout << "File not found" << endl;
        return 0;
    }
    else
      while (datafile)
	{
	  datafile >> row >> column;
	  
	  //if ( row >= 0 && row < 20 && column >= 0 && column < 20)  // checks for compatability     
            grid[row][column] = true;
	}
	cout <<"01234567890123456789\n";
}

/**
 Checks how many surrounding cells are alive for a cell and changes it depending on the game of life rules.
 The changes are stored in a temporary 2D array. Then, it is copied to the main grid array. 
**/
void nextGeneration() {
	
	int neighbors = 0;
	bool tempBoard [column][row];
	
	for (int i = 0; i < column ; i++)
	{
		for (int j = 0; j < row ; j++)
		{
			if ( (i+1) < column && grid[i + 1][j] == true )
			{
				neighbors++;
			}
			if ( (i-1) >= 0 && grid[i - 1][j] == true )
			{
				neighbors++;
			}
			if ( (j+1) < row && grid[i][j+1] == true )
			{
				neighbors++;
			}
			if ( (j-1) >= 0 && grid[i][j-1] == true )
			{
				neighbors++;
			}
			if ( (i+1) < column && (j+1) < row && grid[i+1][j+1] == true )
			{
				neighbors++;
			}
			if ( (i+1) < column && (j-1) >= 0 && grid[i+1][j-1] == true )
			{
				neighbors++;
			}
			if ( (i-1) >= 0 && (j+1) < row && grid[i-1][j+1] == true )
			{
				neighbors++;
			}
			if ( (i-1) >= 0 && (j-1) >= 0 && grid[i-1][j-1] == true )
			{
				neighbors++;
			}
			
			if (neighbors < 2 || neighbors > 3)
			{
				tempBoard[i][j] = false;
			}
			else if (neighbors == 2)
			{
				tempBoard[i][j] = grid[i][j];
			}
			else if (neighbors == 3)
			{
				tempBoard[i][j] = true;
			}
			


			neighbors = 0;
			
		}
	}
	
	for (int i = 0 ; i < column ; i++ )
	{
		for (int j = 0 ; j < row ; j++ )
		{
			grid[i][j] = tempBoard[i][j];
		}
	}
}

//Prints the grid.
void printBoard()
{
	for (int i = 0; i < 20 ; i++ )
	{
		for (int j = 0 ; j < 20 ; j++ )
		{
			if ( grid[i][j] == true )
			{
				cout << "*";
			}
			else
			{
				cout << " ";
			}
			
			if ( j  >= row )
			{
				cout << endl;
			}
		}
	}
}


int main () {	
	int inputcolumn = 20, inputrow = 20;
	string kbd;

	cout << "The Game of Life in\n\n";
       
	//inputcolumn = 20;
	
	//inputrow = 20;
	
	Board (inputcolumn, inputrow);
	printBoard();
	cout << endl;
	
		for(int z = 0; z < 2; z++)
		{	
			cout <<"01234567890123456789\n";
			nextGeneration();
			printBoard();
			cout << endl;
		}
	
	

	
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.