Hey guys,
I'm new to this forum but I could really use some help.
I've been tasked with programming Conway's Game of Life in C++ and compiling in Cygwin (with a makefile). I'm not asking anybody to complete the program for me or anything like that, but I am totally stuck on this one part...
One of the facets of the program is to allow a user to input a text file as a map for the initial grid, rather than using a randomly generated grid.
Here is an example of the format of the .txt file (the numbers and 'X' are purely for example, a file can be any variation of this format):
5 (rows)
6 (columns)
-----X
X--X--
------
-XX---
------
The 'X' represent spaces with live bacteria, and the '-' represent dead spaces.
Although my program compiles just fine through cygwin, when I run the .exe I get a "Segmentation Fault (Core Dumped)" error. I've done extensive google searching to this point, but I've found that this error is generally very specific to the program which it concerns, so other solutions aren't of much help to me.
I don't want to spam you guys with a ton of code, so I'm only including my loadFile function for now. If you need more of the code to help, let me know and I'll post it immediately.
Here's what I have so far in my loadFile function:
Code:
void GamePlay::loadFile(int r, int c, char** newBoard){
int i;
//int r;
//int c;
//char** newBoard;
int len;
int k;
do{
r = 0;
c = 0;
string filePath;
cout << "Please enter a file path" << endl;
cin >> filePath;
DIR* path = opendir(filePath.c_str());
string fpath = filePath.c_str();
dirent* openIn = readdir(path);
string line;
string ext = ".txt";
i = 0;
while(openIn && i != -1){
if(openIn->d_type == DT_REG){
string fileName = openIn->d_name;
string filePath = fpath + fileName;
int begExt = fileName.find_last_of(".");
int endExt = fileName.find_last_of("t");
string extension = fileName.substr(begExt, endExt);
ifstream in(filePath.c_str());
k = 0;
if(in && ext == extension){
getline(in,line);
istringstream(line) >> r;
getline(in,line);
istringstream(line) >> c;
newBoard = new char*[r]; //create multi-d array
for(int a = 0; a < r; ++a){
newBoard[a] = new char[c];
}
while(in.good() && i != -1){
if(k <= r){
if(len == c){
for(int g = 0; g < r; ++g){
getline(in,line);
len = line.size();
char* arr = new char[len];
memcpy(arr,line.c_str(),len);
for(int h = 0; h < c; ++h){
newBoard[g][h] = arr[h];
}
}
}
/*if(len == c){
//newBoard = len*sizeof(char*);
for(int a = 0; a < len; ++a){
newBoard[a] = r;
memcpy(newBoard[a], line, r);
}
}*/
else{
cout << "Your column number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
}
else{
cout << "Your row number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
k++;
}
}
else{
cout << "File invalid. Please try again with a new file." << endl;
i = -1;
}
return;
}
else{
cout << "Invalid file path. Please try again with a new file." << endl;
i = -1;
}
}
}while(i = -1);
}
Thanks!!!
Bookmarks