I'm trying to write a program that will read in a file to a 2-d array and find out how many groupings (blobs) of characters there in the file. It then uses a recursive function to delete the already counted blobs, to avoid recounting. The program compiles fine, but when I run the program it just crashes. Thanks in advance for any help.

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

const int MAXROW = 22;
const int MAXCOL = 72;
void getnoblob(char ba[MAXROW][MAXCOL], int r, int c);
void destroyblob(char ba[MAXROW][MAXCOL], int r, int c);


int main()
{
	char ba[MAXROW][MAXCOL];
	int inputline = 0;
	ifstream myfile("blob.txt");
	myfile.open("blob.txt");
	for (int row=1; row<MAXROW; row++)
	{
	for (int col=1; col<MAXCOL; col++)
	{
		myfile >> ba[row][col];
	}
	}
	myfile.close();
	getnoblob(ba, 1, 1);

}


void getnoblob(char ba[MAXROW][MAXCOL], int r, int c)
{
	int counter = 0;
	for(int r=1; r<MAXROW; r++)
	{
	for(int c=1; c<MAXCOL; c++)
	{
		if (ba[r][c] = 'X')
			destroyblob(ba, r, c);
			counter = counter + 1;
	}
	cout << counter << endl;
	}
}

void destroyblob(char ba[MAXROW][MAXCOL], int r, int c)
{
	ba[r][c] = ' ';
	if(ba[r-1][c-1] != ' ') destroyblob(ba, r-1, c-1);
	if(ba[r-1][c+1] != ' ') destroyblob(ba, r-1, c+1);
	if(ba[r-1][c] != ' ') destroyblob(ba, r-1, c);
	if(ba[r+1][c-1] != ' ') destroyblob(ba, r+1, c-1);
	if(ba[r+1][c+1] != ' ') destroyblob(ba, r+1, c+1);
	if(ba[r+1][c] != ' ') destroyblob(ba, r+1, c);
	if(ba[r][c-1] != ' ') destroyblob(ba, r, c-1);
	if(ba[r][c+1] != ' ') destroyblob(ba, r, c+1);
}

1. Redundant

ifstream myfile(<filename>);
myfile.open(<filename>);

2. Use \[CODE\] \[/CODE\] tags for better visibility of code.

3. Give an example of the contents so that we get a better idea.

  1. Thanks, this is my first programming class so i'm pretty new to this and don't realize things like that haha.
  2. Sorry, i tried using the CODE buttons on the editor, I guess I didn't do it properly.

    include <iostream>
    include <iomanip>
    include <fstream>
    include <string>

    using namespace std;

    const int MAXROW = 22;
    const int MAXCOL = 72;
    void getnoblob(char ba[MAXROW][MAXCOL], int r, int c);
    void destroyblob(char ba[MAXROW][MAXCOL], int r, int c);

    int main()
    {
    char ba[MAXROW][MAXCOL];
    int inputline = 0;
    ifstream myfile("blob.txt");
    myfile.open("blob.txt");
    for (int row=1; row<MAXROW; row++)
    {
    for (int col=1; col<MAXCOL; col++)
    {
    myfile >> ba[row][col];
    }
    }
    myfile.close();
    getnoblob(ba, 1, 1);

    }

    void getnoblob(char ba[MAXROW][MAXCOL], int r, int c)
    {
    int counter = 0;
    for(int r=1; r<MAXROW; r++)
    {
    for(int c=1; c<MAXCOL; c++)
    {
    if (ba[r][c] = 'X')
    destroyblob(ba, r, c);
    counter = counter + 1;
    }
    cout << counter << endl;
    }
    }

    void destroyblob(char ba[MAXROW][MAXCOL], int r, int c)
    {
    ba[r][c] = ' ';
    if(ba[r-1][c-1] != ' ') destroyblob(ba, r-1, c-1);
    if(ba[r-1][c+1] != ' ') destroyblob(ba, r-1, c+1);
    if(ba[r-1][c] != ' ') destroyblob(ba, r-1, c);
    if(ba[r+1][c-1] != ' ') destroyblob(ba, r+1, c-1);
    if(ba[r+1][c+1] != ' ') destroyblob(ba, r+1, c+1);
    if(ba[r+1][c] != ' ') destroyblob(ba, r+1, c);
    if(ba[r][c-1] != ' ') destroyblob(ba, r, c-1);
    if(ba[r][c+1] != ' ') destroyblob(ba, r, c+1);
    }

3.

           X                                XXX
        X                                 XXX
       X          XXXXX
 X
 X
 X
                     XXXXXXX
                     XXXXXXX       XXXXXX

In this case there would be 6 "blobs".

...and the example isn't posting correctly. Basically there are groupings of X's, each X is considered part of the group as long as it touches another X in an adjacent space.

______XXX____________________________________X______
______XXX__________XX_______________________X_______
___________________XX______________________X________
_X_________________XX_____________________X_________
_X_____________________XXXXXXX______________________
_X___XXXX___________________________________________
_____XXXX___________________________________________

In this case there would be 6 blobs, the underscores and just there to represent empty spaces.

Its a very good thing that you are trying. There are many mistakes in your program.

1. Index of arrays always start from 0 and not 1.
2. I believe you intend to read the entire file into your array so that you can operate on it. But your reading part itself is wrong. (Try to print what you have read and you will realize). You can read the file into the array this way :

ifstream myfile;
myfile.open("blob.txt");
for(int row=0;row<MAXROW;row++)
{
    myfile >> ba[row];
}
myfile.close();

4. For get blob and destroy blob put your thoughts as to what you want the function to do on a paper and then try to code the functions.

Happy Coding :)

Thanks for the help so far. I've done some more work on it and i'm still having issues. First off, the reason i had it as

for (int row=1; row<(MAXROW - 1); row++)
	{
		for (int col=1; col<(MAXCOL - 1); col++)

is that the 1st and last row and column are supposed to be blank for when the destroy function checks for values on the boundary. The txt file is 20x70, whereas I have the array as 22x72. When i try to print out the array I can see that it isn't being read it correctly, but I can't figure out why. Also, i'm not sure where i'm going wrong with my destroy and getblob functions. Any help with this would be appreciated! Here's what I have so far as of now:

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

const int MAXROW = 22;
const int MAXCOL = 72;
void getnoblob(char ba[][MAXCOL]);
void destroyblob(char ba[][MAXCOL], int r, int c);
void display(char ba[][MAXCOL]);
void readfile (string filename, char ba[][MAXCOL]);


int main()
{
	char ba[MAXROW][MAXCOL];
	string filename = "blob.txt";
	readfile(filename, ba);
	display(ba);
	getnoblob(ba);

}


void readfile (string filename, char ba[][MAXCOL])
{
	ifstream myfile;
	myfile.open("blob.txt");
	for (int row=1; row<(MAXROW - 1); row++)
	{
		for (int col=1; col<(MAXCOL - 1); col++)
		{
			myfile >> ba[row][col];
		}
	}
	myfile.close();
}

void display(char ba[][MAXCOL])
{
	for (int row=1; row<(MAXROW - 1); row++)
	{
		for (int col=1; col<(MAXCOL - 1); col++)
		{
			cout << ba[row][col];
		}
	}
}



void getnoblob(char ba[][MAXCOL])
{
	int counter = 0;
	for(int r=1; r<(MAXROW - 1); r++)
	{
	for(int c=1; c<(MAXCOL - 1); c++)
	{
		if (ba[r][c] = 'X')
			destroyblob(ba, r, c);
			counter = counter + 1;
	}
	}
	cout << "The number of blobs is: " << counter << endl;
}

void destroyblob(char ba[][MAXCOL], int r, int c)
{
	if (r < 1 || r > MAXROW-1 || c < 1 || c > MAXCOL - 1) return;
	ba[r][c] = ' ';
	if(ba[r][c+1] != ' ') destroyblob(ba, r, c+1);
	if(ba[r-1][c+1] != ' ') destroyblob(ba, r-1, c+1);
	if(ba[r-1][c] != ' ') destroyblob(ba, r-1, c);
	if(ba[r-1][c-1] != ' ') destroyblob(ba, r-1, c-1);
	if(ba[r][c-1] != ' ') destroyblob(ba, r, c-1);
	if(ba[r+1][c-1] != ' ') destroyblob(ba, r+1, c-1);
	if(ba[r+1][c] != ' ') destroyblob(ba, r+1, c);
	if(ba[r+1][c+1] != ' ') destroyblob(ba, r+1, c+1);
}
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.