Hi guys, i need to make the Game of life in C++. I get a run time error when i run this program, it compiles fine. This is the .cpp file in the project. Dont know if you need the driver or header to spot my mistake.

#include <iostream>
#include <fstream>

#include "life.h"

using namespace std;


string *text; 
int numRows;
int numCols;
int neighbour = 0;
int rowup;
int rowdown;
int colup;
int coldown;




void populateWorld (string fileName)
{
    
    const int SIZE_INCREMENT = 3;
    char line[80];
    numRows = 0;
    numCols = 0;
    
    ifstream inFile;
    inFile.open(fileName.data());
        
    text = new string[SIZE_INCREMENT];
    
    while ( inFile.getline(line,80) ) 
    {
         
          text [numRows] = line;
          numCols = text[numRows].length();
                  
          cout << text[numRows] << text[numRows].length() << endl;
          numRows++;      


          if (numRows % SIZE_INCREMENT == 0) 
          { //time to resize
          
             cout << "resize at " << numRows << endl;
          
             string *temptext = new string[numRows + SIZE_INCREMENT];
             
             for (int i = 0; i < numRows; i++) 
             {
                 temptext[i] = text[i];
             }
          
             //free the text memory
             delete [] text;         
             
             text  = temptext;
                        
          }
                     
   }
}

void findNeighbours(int row, int col)
{
     
     string tempc = "";     
     string tempd = ""; 
     string tempe = ""; 
     string tempf = ""; 
      
     rowup = row++;
     rowdown = row;
     colup = col++;
     coldown = col;
     
     
     if ( text [row+1][col] == true)
     {
       neighbour += 1;  
     }
     if ( text [row-1][col] == true) 
     {
       neighbour += 1;
     }
     if ( text [row][col+1] == true)
     {
       neighbour += 1;
     }
     if ( text [row][col-1] == true)
     {
       neighbour += 1; 
     }
}

//Prints out structure as if it were 2D array of characters

void showWorld() 
{
   
     //show the text
     for(int row=0; row < numRows; row++) {  
        for (int col=0; col < numCols; col++) {  
           cout << text[row][col];   
        }         
        
        cout << endl;                           
     }
    
     cout << endl;
    
    
}


//"encrypts" text by adding one to each character
// Note: we create a duplicate of the space in prep for the Life program

void iterateGeneration() 
{
    
    string *newtext = new string[numRows];  

     for(int row=0; row < numRows; row++) 
     {         
         string temp = ""; //create a storage place for the next line   
         
         
         for (int col=0; col < numCols; col++) 
         { 
            
            findNeighbours(row, col);
          
                     
            if ((text[row][col]) == 1)
            {
               if (neighbour >= 4)
               {
                 temp += (char) (text[row][col] -1);
               }
               if (neighbour <= 1)
               {
                 temp += (char) (text[row][col] -1);
               }
            }
            
            else if (neighbour == 3) 
            {
                temp += (char) (text[row][col] +1);
            } 
            
            else
                temp += (char) (text[row][col]);
                
         }   
            
         newtext[row] = temp; 
    }
  
    delete [] text;
    
    text = newtext;
        
    return;
}

Recommended Answers

All 4 Replies

It would be appreciated if anyone can point out where the mistakes are, guys. Thanks.

Yes you should post life.h, well how can WE run the program, if you don't! -- and you want help with a runtime error!!

You seem to have made a mistake in iterateGeneration(). You create an initial line text that has zero characters in it. Then you call findNeighbours, however, then you get into a conditional test that can easily leave temp without any additional characters. THEN newtext is short.

That then causes a runtime problem on the next loop, since text is assigned to new text, and you then access text, assuming it is the correct length.

To demonstrate that: Add this to the end of iterateGeneration

for(int i=0;i<numRows;i++)
  if (text[row].length()!=numCols)
     std::cerr<<"Error with line "<<i<<std::endl;

life.h

#ifndef crypto_h
#define crypto_h

#include <iostream>
#include <fstream>

#include <string>

using namespace std;

//declarations for functions
void populateWorld(string FILE_NAME);

void findNeighbours (int, int);

void showWorld();

void iterateGeneration();


#endif

.cpp

#ifdef linux 
#define LINUX true
#define WINDOWS false
#endif

#ifdef __WIN32__ 
#define LINUX false 
#define WINDOWS true 
#endif


#include <iostream>
#include <fstream>

#include "life.h"

const string FILE_NAME = "starting_grid.txt";


using namespace std;

const int NUM_GENERATIONS = 2; //set to a smaller number for debugging

int main() {
    
    populateWorld(FILE_NAME);
        
    showWorld();  
  
    for (int iteration = 0; iteration < NUM_GENERATIONS; iteration++) {
   
        if (WINDOWS)
           system("cls"); //Windows only
        else
           system("clear"); //Linux only
    
        iterateGeneration();
    
        showWorld();
    } 

    if (WINDOWS)
        system("PAUSE");
    
    return 0;
}

I have looked at the code a little more. I also ran a simple test case, and there are a few problems.

The first is findNeighbours. That method goes beyond the boundary of the rows/columns.It produces an seg-fault when it does it [and if it didn't would give you the wrong answer. You also increment row and col in the function, and therefore get the wrong answer anyway. Also neighbour is not zeroed ....

You use true as a char test. BUT space is not tested, so you have to enter null characters into the input file... So assign the character to a space if it is empty [ or maybe a . ]. Then test for that and put an X in other spaces for example.

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.