Thread: error in program help ASAP pleeease

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Talking error in program help ASAP pleeease

    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

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    karen-

    that's not what the program does.
    This was not a program designed to count character in a file.

    this program finds "blobs" with the char '*' in a grid.
    plus you've declared const cols, rows at the top and then redeclared rows and cols in the main part of the program and have not set any values for them.
    Only use one or the other.

    You still have to modify the program to look for the characters in the file.

    This program was merely to guide you in a way to attack the problem.

  3. #3
    Unregistered
    Guest

    I give up

    Thanks anyway, I tried my best just couldn't work it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help !!! need to write program asap
    By Adamakl in forum C Programming
    Replies: 3
    Last Post: 11-12-2006, 11:03 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM