I am getting a segmentation error in this file, I think it has something to do with the row, rows, col, cols part....Tearing my hair out here...Can anyone help, its due this afternoon. Counts chars in a file.
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
const int rows = 26;
const int cols = 51;
static ifstream infile;
//Open file
void Openfile(void)
{
const int fileSIZE=100;
char filename[fileSIZE];
cout << "Enter data file name:" << endl;
cin.getline(filename, ios::in | ios::nocreate);
if(!infile.good())
{
cout << "Error:unable to open" << filename << endl;
exit(1);
}
}
// class for blob
class Blob
{
public:
Blob(int row, int col);
bool Check_grid(); //searches array for '*'
int find_blobs(); //finds blobs in the grid
void findblob(int row, int col);
private:
int rows, cols, blobs; //number of rows, columns, and blobs
char pt[26][51]; //pointer to array
};
Blob::Blob(int row, int col)
{
rows = row;
cols = col;
blobs = 0;
}
//check grid for asterisks
bool Blob::Check_grid()
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (pt[i][j] == '*')
return true;
}
return false;
}
//finds blobs in cols and rows
int Blob::find_blobs()
{
while (Check_grid())
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
if (pt[i][j] == '*')
{
blobs++;
pt[i][j] = ' ';
findblob(i, j);
}
}
}
return blobs;
}//end function
//finds blobs in surrounding cells
void Blob::findblob(int i, int j)
{
if (pt[i+1][j] == '*')
{
pt[i+1][j] = ' ';
findblob(i+1, j);
}
if (pt[i+1][j+1] == '*')
{
pt[i+1][j+1] = ' ';
findblob(i+1, j+1);
}
if (pt[i+1][j-1] == '*')
{
pt[i+1][j-1] = ' ';
findblob(i+1, j-1);
}
if (pt[i][j+1] == '*')
{
pt[i][j+1] = ' ';
findblob(i, j+1);
}
if (pt[i-1][j-1] == '*')
{
pt[i-1][j-1] = ' ';
findblob(i-1, j-1);
}
if (pt[i-1][j+1] == '*')
{
pt[i-1][j+1] = ' ';
findblob(i-1, j+1);
}
if (pt[i-1][j] == '*')
{
pt[i-1][j-1] = ' ';
findblob(i-1, j-1);
}
if (pt[i][j-1] == '*')
{
pt[i][j-1] = ' ';
findblob(i, j-1);
}
}
//start of main
void main()
{
int rows, cols;
Openfile();
Blob obj(rows, cols);
cout << "Here is the grid:" << endl;
cout << "\nThere are " << obj.find_blobs() << " blobs in it." << endl;
} //end of main