struct in class not working properly

I started doing a program that is supposed to solve crosswords ( search for words in all directions ).
So basicaly I have a field [X:Y] , there is a letter on every location ( '0' for empty ) and I have to keep track if that letter was used in a word or is left and that's why I made that a struct outside the class and I use it as a variable in the main class.
I also add new words that I'm searching for. I made a struct for that too because I have to keep track if the word was found or not and it also has to contain the word itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct csItem
{
	char item;
	bool state;
};

struct csSearch
{
	std::string word;
	bool found;
};

class csField
{
public:

	csField(unsigned short sizeXY);
	csField(unsigned short sizeX, unsigned short sizeY);
	void initialize();
//code...

private:
	csItem **item;
	csSearch *search;


It looks like this , csField is the class , the whole field. csItem is the smallest part of the field, it has coordinates [x:y].
csSearch is the struct that contains the words that I'm looking for.

I add new words with the method pushWord :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void pushWord(std::string word)
{
	csSearch *temp = NULL;

	if ( words != 0 )
	{
		temp = new csSearch[words];

		for ( int i = 0 ; i < words ; i++ )
		{
			temp[i].word = search[i].word;
			temp[i].found = search[i].found;
		}

		delete[] search;
		words++;
		search = new csSearch[words];

		for ( int i = 0 ; i < words-1 ; i++ )
		{
			search[i].word = temp[i].word;
			search[i].found = temp[i].found;
		}

		delete[] temp;

		search[words-1].word = word;
	}
	else
	{
		words++;
		search = new csSearch[words];
		search[words-1].word = word;
	}
}


words is a integer variable inside the class csField that keeps track of how many words are there.

The algoritm that is searching for the words is not wrong, actually I was suprised that it is working... The problem is something i've never met before and I don't understand what is happening , it's working just partially and I tried to solve it but without success.
If I show the whole field that is read from a text file it is working without any problem, the item[][] contains the right letters and the state variable is initialized to false.
The same applies for search[], I add all the words and the string inside the struct contains all of them without problem.

But when I need to change the value of any boolean of the struct, be it search, indicating that the word was found or be it item, indicating that the letter was used in a word, it won't change the value to true.

Here is the code in which I change the value of search :
(It is inside a method of the class csField)

1
2
3
4
if ( found == true )
{
	this->search[w].found = true;
}


And here is the part where I change the item value :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool matchRIGHT(std::string word,int x,int y)
{
	int length = word.length();
	bool match = true;
	char letter = '0';

	for ( int sx = 0 ; sx < sizeX && sx < length ; sx++ )
	{
		letter = this->getItemValue(x+sx,y);

		if ( word[sx] != this->getItemValue(x+sx,y) )
			match = false;
	}

	if ( match == true )
	{
		std::cout << word << " was found![matchRIGHT]" << std::endl;

		for ( int sx = 0 ; sx < sizeX && sx < length ; sx++ )
		{
			//change state of all items used
			this->setItemState(x+sx,y,true);
		}
	}

	return match;
}


1
2
3
4
char getItemValue(int x, int y) { return item[x][y].item ; }
bool getItemState(int x, int y) { return item[x][y].state ; }
void setItemValue(int x, int y, char value) { item[x][y].item = value; }
void setItemState(int x, int y, bool state) { item[x][y].state = state; }


Both of the variables should change it's value from FALSE to TRUE but when I put them to the output they are still false.
But if I have an object of the class, let's say csField Alpha(30);
if I change the value in main() like, Alpha.setItemState(0,0,true);
It will change the variable, but if I do it in the class using this-> it won't change anything. I was using the debugger to make sure it goes to the line where it is supposed to change it and it goes to the line, no error, but the value is left unchanged. Why is this so ?
Is matchRight a member of the class csField? Same with pushWord. From your definition it's apears they are distinct functions, not members of those classes. In this case "this" has no meaning. Could you please clarify that?
Last edited on
Both of them are members of csField.
This is the header file. Also I'm not sure if it's important but I'm writing is as seperate files having a header file and a source file but currently I'm writing everything into the header file and will convert all the big methods into the seperate .cpp file.
All the match_ methods are private because they are called by solve() and the user shouldn't call them seperately. I made seperate methods because solve() was getting too chaotic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef _CSWORD_H_
#define _CSWORD_H_

struct csItem
{
	char item;
	bool state;
};

struct csSearch
{
	std::string word;
	bool found;
};

class csField
{
public:

	csField(unsigned short sizeXY);

	csField(unsigned short sizeX, unsigned short sizeY);

	void initialize();

	void loadField(std::string filename);

	char getItemValue(int x, int y) { return item[x][y].item ; }
	bool getItemState(int x, int y) { return item[x][y].state ; }
	void setItemValue(int x, int y, char value) { item[x][y].item = value; }
	void setItemState(int x, int y, bool state) { item[x][y].state = state; }

	void pushWord(std::string word);
	void popWord();
	std::string getWord(unsigned short word)
	{
		return search[word].word;
	}

	void solve();
	
private:

	bool matchRIGHT(std::string word,int x,int y);
	bool matchLEFT(std::string word,int x,int y);
	bool matchDOWN(std::string word,int x,int y);
	bool matchUP(std::string word,int x,int y);
	bool matchRIGHT_UP(std::string word,int x,int y);
	bool matchLEFT_UP(std::string word,int x,int y);
	bool matchRIGHT_DOWN(std::string word,int x,int y);
	bool matchLEFT_DOWN(std::string word,int x,int y);

	unsigned short int sizeX, sizeY;
	unsigned short int words;

	csItem **item;
	csSearch *search;
};

#endif 
Last edited on
OK, I found the mistake. I'm sorry to waste everyones time , this is the worst mistake that I made in my whole life...
The method solve() is supposed to change the values of the letters and the found words but in my program I print out the values first AND THEN I call the solve() method... I don't understand how could I oversee this. Sorry
Topic archived. No new replies allowed.