I'm having a problem with my code. I am doing Where's Waldorf on the UVA site. I understand how I want to tackle this project, but am unable to get past a seg fault.

My strategy is to pass the multi-dimensional char array of the search board to a function along with a position in that board to search and have that function send back an array of 8 strings that contain the possible words in all 8 directions you can go in the search. Then with those 8 strings, I go to another function that checks to see whether any of the search words are in any of those strings. That function returns a bool and based on it's answer, it will put those coordinates into another part of the words to find array and then go to the next coordinate until all the words have been found.

I don't have all the code written yet, but I do have code for finding the 8 strings from the search, and the searching of those 8 strings for whether one of the words to be found is there.

My problem is that I keep getting a seg fault and I don't know why. If anyone can narrow it down for me that would help. It compiles in Netbeans using cygwin, but when it actually opens in terminal, I get the error message:
/cygdrive/C/Program Files/NetBeans 6.9.1/ide/bin/nativeexecution/dorun.sh: line
33: 2808 Segmentation fault (core dumped) sh "${SHFILE}"
Press [Enter] to close the terminal ...

thanks for any and all help.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

//function to see if the words are in one of the 8 strings
bool check_word(string _words_[][2], int num_words, string find[], int *_pos_);

bool check_word(string _words_[][2], int num_words, string find[], int *_pos_)
{
    for(*_pos_ = 0; *_pos_ < num_words; *_pos_++)
    {
        for(int j = 0; j < 8; j++)
        {
            if(find[j].find(_words_[*_pos_][0]))
            {
                return true;
            }
        }
    }
    return false;
}

//function to take in the word search, and spit out 8 strings for all directions
void find_word(int x, int y, int _n, int _m, string find[], char word_find[][11]);

void find_word(int x, int y, int _n, int _m, string find[], char word_find[][11])
{
    //x - position in word search
    //y - position in word search
    //n - max x position
    //m - max y position
    //find[] - for the 8 strings found
    //word_find[][] - word search arrays
    int temp_x = x;
    int temp_y = y;
    while(temp_x >= 1)//left
    {
        find[0] += word_find[temp_x][y];
        temp_x --;
    }
    temp_x = x;
    while(temp_x >= _n)//right
    {
        find[1] += word_find[temp_x][y];
        temp_x ++;
    }
    temp_x = x;
    while(temp_y >= 1)//up
    {
        find[2] += word_find[x][temp_y];
        temp_y --;
    }
    temp_y = y;
    while(temp_y <= _m)//down
    {
        find[3] += word_find[x][temp_y];
        temp_y ++;
    }
    temp_y = y;
    while((temp_x >= 1) &&(temp_y >= 1))//up-left
    {
        find[4] += word_find[temp_x][temp_y];
        temp_x --;
        temp_y --;
    }
    temp_x = x;
    temp_y = y;
    while((temp_x <= _n) &&(temp_y >= 1))//up-right
    {
        find[5] += word_find[temp_x][temp_y];
        temp_x ++;
        temp_y --;
    }
    temp_x = x;
    temp_y = y;
    while((temp_x >= 1) &&(temp_y <= _m))//down--left
    {
        find[6] += word_find[temp_x][temp_y];
        temp_x --;
        temp_y ++;
    }
    temp_x = x;
    temp_y = y;
    while((temp_x <= _n) &&(temp_y <= _m))//down-right
    {
        find[7] += word_find[temp_x][temp_y];
        temp_x ++;
        temp_y ++;
    }
    temp_x = x;
    temp_y = y;

    cout << "inside find" << endl;
    for(int i = 0; i < 8; i++)
    {
        cout << find[i] << endl;
    }

    //return check_word(_words, num_words, find, &*_pos);
}




int main(int argc, char** argv) {
    int n = 11;
    int m = 8;
    int x, y, pos = 0;
    int num_words = 4;
    string guess[8];
    char word_find[8][11] = {{'a','b','c','d','e','f','g','h','i','g','g'},{'h','e','b','k','w','a','l','d','o','r','k'},{'f','t','y','a','w','a','l','d','o','r','m'},{'f','t','s','i','m','r','l','q','s','r','c'},{'b','y','o','a','r','b','e','d','e','y','v'},{'k','l','c','b','q','w','i','k','o','m','k'},{'s','t','r','e','b','g','a','d','h','r','b'},{'y','u','i','q','l','x','c','n','b','j','f'}};
    string words[4][2] = {{"waldorf","0"},{"bambi","0"},{"betty","0"},{"dagbert","0"}};
    
   find_word(x, y, n, m, guess, word_find);

    return 0;
}

///cygdrive/C/Program Files/NetBeans 6.9.1/ide/bin/nativeexecution/dorun.sh: line
//33:  2808 Segmentation fault      (core dumped) sh "${SHFILE}"
//Press [Enter] to close the terminal ...

Recommended Answers

All 2 Replies

line 116: variables x and y have not been initialized to anything.

line 116: variables x and y have not been initialized to anything.

I was going to reply back that they were at line 110, then I decided that I would go try and set them to 0 first. which is what was wrong. I thought that if I set one of the variables in the declaration of several variables to something, then all of them would be that something. I learned a valuable lesson today.

You just became my hero. Thank you!!

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.