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