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;
    }
}

Recommended Answers

All 2 Replies

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..****.***....*....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.