Just registered but you guys have been a big help for the whole semester. I have what seems to be a simple problem but I'm just stuck. My assignment is a game of life sim that prompts the user for an input file, and then reads integers from the file that represent coordinates of the cells (row/column). After that, it does its thing until the user decides it should stop. I designed it originally to get the values from the keyboard (enter two integers between 1-20 with a space inbetween until negatives are read) because I thought it would be easy to change my cin's to fin's but I'm having some trouble. As of now, the file seems to open but the grid is blank. Here's the section of relevant code:

int main()
{
    ifstream datafile;
    string filename;
  
  cout << "The input values from the file will begin the first generation." << endl << endl;
  cout << "Follow the prompts to create new generations or terminate the program." << 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] = false;

  int row, col;

  // Prompt user for filename.
  
    cout << "Please enter the file name containing input values. " << endl; 
    cout << endl;
    cin >> filename;

    cout << fixed << showpoint;

    datafile.open(filename.c_str());
    if (!datafile)
    {
        cout << "File not found" << endl;
        return 1;
    }
    while (datafile)
    {
        datafile >> row >> col;
    }
    if ( row>=0 && row<Size && col>=0 && col<Size)  // if legal row-col
      Grid[row][col] = true;
   while ( row != -1);

  displayGrid(Grid);

And here's how the input file is setup:

1 2
3 3
5 5
13 4
17 5
12 12
5 19
-1 -1

Any help would be appreciated. Thanks in advance.

while (datafile)
    {
        datafile >> row >> col;
    }
    if ( row>=0 && row<Size && col>=0 && col<Size)  // if legal row-col
      Grid[row][col] = true;

In the above code you need to move that if statement to be within the while statement. As it is, your program reads the whole file but does nothing with the numbers.

Thanks a lot. It's reading the file just fine now but I noticed another problem after running through a couple generations. I neglected to include a command to populate a cell with three neighbors. No kids means everyone just slowly dies out. Here's the relevant code (let me know if you need more):

void nextGen(bool World[][Size], bool tempWorld[][Size])
{
  int neighborcount;
  for (int i=0; i < Size; i++)
    for (int j=0; j < Size; j++) {
      neighborcount = getNeighborCount(World,i,j);
      if (neighborcount == 2)             // 2 neighbors 
    tempWorld[i][j] = World[i][j];  // stays alive.
      else if (neighborcount == 3)        
    tempWorld[i][j] = true;         // 3 neighbors:
                                        // stays alive

      else tempWorld[i][j] = false;      // 1 and 4+ neighbors:
    }                                    // dead

  // copy tempWorld to World
  for (int i=0; i < Size; i++)
    for (int j=0; j < Size; j++)
      World[i][j] = tempWorld[i][j];
}

int getNeighborCount(const bool World[][Size], int row, int col)

// Counts neighbors in a row/column pair
{

  int count=0;

  if (row > 0) {
    if (col > 0 && World[row-1][col-1] == true)          
      count++;
    if (col < Size-1 && World[row-1][col+1] == true)     
      count++;
    if (World[row-1][col] == true)                       
      count++;
    
  }

  if (row < Size - 1) {
    if (col > 0 && World[row+1][col-1] == true)          
      count++;
    if (col < Size - 1 && World[row+1][col+1] == true)  
      count++;
    if (World[row+1][col] == true)                       
      count++;
  }

  if (col > 0 && World[row][col-1] == true)            
    count++;
  if (col < Size - 1 && World[row][col+1] == true)       
    count++;

  return count;
}

I'm really not sure how to tackle the problem and any ideas would be helpful.
Thanks in advance and thanks again Ancient Dragon.

i have code for basically this exact thing in VB but using arrays if you are interested

Thanks a lot. It's reading the file just fine now but I noticed another problem after running through a couple generations. I neglected to include a command to populate a cell with three neighbors. No kids means everyone just slowly dies out. Here's the relevant code (let me know if you need more):

I don't think I understand. What does this code do?

if (neighborcount == 2)             // 2 neighbors 
    tempWorld[i][j] = World[i][j];  // stays alive.
      else if (neighborcount == 3)        
    tempWorld[i][j] = true;         // 3 neighbors:
                                        // stays alive

      else tempWorld[i][j] = false;      // 1 and 4+ neighbors:

Doesn't it test for 3 neighbors?

And please format your code better -- it's a little hard to follow. You need more {}'s and proper indentation to make it easier to read.

i have code for basically this exact thing in VB but using arrays if you are interested

Sure I'll take a look. I'd like to see another way of doing it. Thanks.

I don't think I understand. What does this code do?

calls a getNeighborCount function and loops a local array through to a temporary world. This part decides in the temp world if the cell will live or die. After that decision is made, the temporary world gets copied over to the next generation.

if (neighborcount == 2)             // 2 neighbors 
    tempWorld[i][j] = World[i][j];  // stays alive.
      else if (neighborcount == 3)        
    tempWorld[i][j] = true;         // 3 neighbors:
                                        // stays alive

      else tempWorld[i][j] = false;      // 1 and 4+ neighbors:

Doesn't it test for 3 neighbors?

Yes it does, but the three cells should populate a fourth. The repopulating thing is bugging me.

And please format your code better -- it's a little hard to follow. You need more {}'s and proper indentation to make it easier to read.

I'm reformatting right now. I agree that it is hard to read and apologize. I'll repost as soon as I'm done. Thanks for telling me. I'm sure that would have cost me points.

Here's the whole thing. I'm submitting it today so if there's any fine tuning that needs to be done or if you'd just like to yell at me feel free. WaltP- I tried spacing stuff out and lining things up but really didn't add many brackets because they've been a sore spot with me. Any suggestions you (or anyone else) have further regarding the formatting are welcome and appreciated. Thanks again everyone.

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

const int Size = 20;
void displayGrid(const bool [][Size]);
void nextGen(bool [][Size], bool [][Size]);
int getNeighborCount(const bool [][Size], int, int);

int main()
{
    ifstream datafile;
    string filename;
  
  cout << "The input values from the file will begin the first generation." << endl << endl;
  cout << "Follow the prompts to create new generations or terminate the program." << 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] = false;

  int row, col;

  // Prompt user for filename.
  
    cout << "Please enter the file name containing input values. " << endl; 
    cout << endl;
    cin >> filename;

    cout << fixed << showpoint;

    datafile.open(filename.c_str());
    if (!datafile)
    {
        cout << "File not found" << endl;
        return 1;
    }
    while (datafile)
    {
        datafile >> row >> col;
    
    if ( row>=0 && row<Size && col>=0 && col<Size)  // checks for compatability
      Grid[row][col] = true;
    }
   while ( row != -1);
    
  displayGrid(Grid); 

  char answer; 
  
  bool temp[Size][Size];   
  
  cout << "Display next generation? Y/N ";
  
  cin >> answer;
  
  while (answer == 'y' || answer == 'Y') 
  {
    nextGen(Grid, temp);
    displayGrid(Grid);
    cout << "Display next generation? Y/N ";
    cin >> answer;
  }

  return 0;
}
 
void displayGrid(const bool World[][Size])
{
  cout << endl;

  for (int i=0; i < Size; i++) 
  {
  for (int j=0; j < Size; j++)
      
      if (World[i][j] == true)
    
          cout << "X ";
      
      else cout << ". ";
         
         cout << endl;
  }
}

// Makes a temporary generation based on the current grid. Calls the 
// getNeighborCount function and makes a live/die decision. Then the 
// temporary world gets copied over to a new generation.

void nextGen(bool World[][Size], bool tempWorld[][Size])
{
  int neighborcount;

  for (int i=0; i < Size; i++)

  for (int j=0; j < Size; j++) 
  {
  neighborcount = getNeighborCount(World,i,j);
  
  if (neighborcount == 2)             // 2 neighbors stays alive
      tempWorld[i][j] = World[i][j];  
      
  else if (neighborcount == 3)        
    tempWorld[i][j] = true;         // 3 neighbors stays alive
                                        

  else tempWorld[i][j] = false;      // 1 and 4+ neighbors = dead
    }                                    

  // copy tempWorld to World
  {
  for (int i=0; i < Size; i++)
    
  for (int j=0; j < Size; j++)
      
  World[i][j] = tempWorld[i][j];
}
}

int getNeighborCount(const bool World[][Size], int row, int col)

// Counts neighbors in a row/column pair
{

  int count=0;

  if (row > 0) 
  {
    
  if (col > 0 && World[row-1][col-1] == true)          
      count++;
  
  if (col < Size-1 && World[row-1][col+1] == true)     
      count++;

  if (World[row-1][col] == true)                       
      count++;
    
  }

  if (row < Size - 1) 
  {
    
  if (col > 0 && World[row+1][col-1] == true)          
      count++;

  if (col < Size - 1 && World[row+1][col+1] == true)  
      count++;

  if (World[row+1][col] == true)                       
      count++;
  }

  if (col > 0 && World[row][col-1] == true)            
    count++;

  if (col < Size - 1 && World[row][col+1] == true)       
    count++;

  return count;
}

I tried spacing stuff out and lining things up ...

Does look better. Read this tutorial for more information.

... but really didn't add many brackets because they've been a sore spot with me.

That's like saying "I want to be a writer, but punctuation confuses me so I won't use much." Simply put brackets around every statement that could be a compound statement. ( if , for , while , etc.)

Any suggestions you (or anyone else) have further regarding the formatting are welcome and appreciated. Thanks again everyone.

Look at good code. Check out the code from poster's here that have a high post count. See what they do and compare it to the tutorial above.

I'll work on it. Thanks for the pointers WaltP. I got bracket-shy because the missplaced bracket in the OP was silly and time-consuming. But you are right. Thanks again.

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.