Word Search

I need to make a word search program that takes in a txt file like this

M J N W B X H S J P L W P I J
U T G U I A C L A E Z E D F B
J J K Z J J L Y T X E T M D W
F U N C T I O N A L P Z L V W
T G S C R U M A J O V H A G L
A J Z I E S U U I S Q Z N A I
Z N A T W Q Z L D H Z L D W Q
E H A H B I H K I O U Y E J G
D Z H G T J L J Q B G F R Z Q
U E A E K Y B L F I E H T D S
X V M G V W Y F I X C U L D J
F S D G F X Z L C N M R G L M
D P F Z R N K U T G G D S U S
K G I T M P S O L L T G A Z N
W R U B T E W T R T V R Z U A


EXCUSE
FUNCTIONAL
GOD
LANDER
TEXT
WILLING

and finds the word and outputs their position in another file
Any ideas on how I would do this?

Last edited on
Well, you can start by reading in the file into a 2D array, and then looking for the start letters of every word in each cell,,, and then searching in all directions for the next letter in the word, etc.
I tried putting in a 2D array but it isnt working

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
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
const int ROWS=15;
const int COLUMNS=15;
char table[ROWS][COLUMNS];
int MAX=10;
int input=0;
fstream inputFile ("wordsearch_data.txt");

void initialize(){

    for (int i=0; i<ROWS; i++)
    {

        for (int j=0; j<COLUMNS; j++)
            table[i][j] =inputFile;
    }

}
void displayConfig()
{
    for (int i=0; i<ROWS; i++)
    {
        for (int j=0; j < COLUMNS; j++)
        {

            cout << table[i][j] << inputFile;

        }

    }
    cout << endl;
}

int main(){
string words[MAX];
int i;
initialize();
displayConfig();
fstream inputFile;
inputFile.open("wordsearch_data.txt");

}
Last edited on
You're not doing your file I/O correctly. really, you're better off having initialize() taking a string with the file name:
void initialize(const char* filename)

then you can call it as:
void initialize("wordsearch_data.txt")
and inside you can open, read, and close the file.

You need to use the extraction operator on your stream when reading from file:
inputFile >> table[i][j];.

Also, it doesn't make any sense to insert inputFile on cout like you do in line 29. It should be just
cout << table[i][j]];
For formatting you can print a space between the characters, and in the outer for loop you should print a newline.

Finally, remove all the other fstream variables you have, just declare the fstream object locally in Initialize(). After you've read in the input, you don't need the file anymore.
Thanks that helped me get it working, now that I got that I am trying to get the words within the array, any idea on what I am doing wrong? When it outputs the words it just outputs letters.
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
61
62
63
64
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;
const int ROWS=15;
const int COLUMNS=15;
char table[ROWS][COLUMNS];
const int MAX=5;
int input=0;
string words[MAX];


void initialize(){
fstream inputFile;
inputFile.open("wordsearch_data.txt");
    for (int i=0; i<ROWS; i++)
    {

        for (int j=0; j<COLUMNS; j++)
            inputFile>>table[i][j];

    }

}
void displayConfig()
{
    for (int i=0; i<ROWS; i++)
    {
        for (int j=0; j < COLUMNS; j++)
        {

            cout << table[i][j];

        }
cout << endl;
    }

}
int wordcheck(){
fstream inputFile;
inputFile.open("wordsearch_data.txt");
string word;

for(int k=0; k<MAX; k++){
    inputFile>>table[15][15]>>words[k];
    cout<<words[k];
}

    }


int main(){

fstream inputFile;
inputFile.open("wordsearch_data.txt");

initialize();
displayConfig();

wordcheck();


}
I misunderstood what your input file was supposed to look like. Since you need the filestream in both functions, you should pass it by reference:
1
2
void initialize(fstream &inputFile)
int wordcheck(fstream &inputFile)


then you should remove the lines:
1
2
fstream inputFile;
inputFile.open("wordsearch_data.txt");

from everywhere except your main().

The reason you aren't reading in your words correctly is because when you re-open the file in wordcheck(), you start at the beginning of the file again, and you would have to read and discard the whole grid of letters before reading your words.

If you pass in a file stream by reference which is already open, once you read in the grid of letters you can then continue reading the words.
Thank you so much for your help. I am trying to check the array for the words now but every time I run it the program crashes. I used the return statements as just a quick reference to see if it was working.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;
const int ROWS=15;
const int COLUMNS=15;
char table[ROWS][COLUMNS];
const int MAX=5;
int input=0;
string words[MAX];


void initialize(fstream &inputFile){

    for (int i=0; i<ROWS; i++)
    {

        for (int j=0; j<COLUMNS; j++)
            inputFile>>table[i][j];
    }

}
void displayConfig()
{
    for (int i=0; i<ROWS; i++)
    {
        for (int j=0; j < COLUMNS; j++)
        {

            cout << table[i][j];

        }
cout << endl;
    }

}
int wordcheck(fstream &inputFile){


for(int k=0; k<MAX; k++){
    inputFile>>table[15][15]>>words[k];
    cout<<words[k]<<endl;
}

    }


int checkRow(fstream &inputFile, int i)
{

    string temp = "";
    for(int j=0; j<15 ; j++)
        temp += table[i][j];

    if (temp.find(words[5]) != string::npos){
        return 0;}

        return 1;

}


int checkColumn(fstream &inputFile, int k)

{

    string temp = "";
    for(int t=0; t<15 ; t++)
        temp += table[t][k];


    if (temp.find(words[5]) != string::npos){
    return 0;}

    return 1;
}

int checkRightDiagonal(fstream &inputFile, int i, int j)
{
    string temp = "";

    while (i<15 && j<15)
    {
        temp += table[i][j];
        i++;
        j++;
    }
    if (temp.find(words[5]) != string::npos){
    return 0;}

    return 1;
}

int checkLeftDiagonal(fstream &inputFile, int i , int j)
{
    string temp = "";

    while (i<15 && j>=0)
    {
        temp += table[i][j];
        i++;
        j--;
    }
    if (temp.find(words[5]) != string::npos){
    return 0;
    }
    return 1;
}


int checkConfiguration(fstream &inputFile)
{

    for(int row=0; row<15 ; row++)
    {
        int result = checkRow(inputFile, row);
        if (result != 0)
        {
            return 20;
        }
    }
    for(int column=0; column<15 ; column++)
    {
        int result = checkColumn(inputFile, column);
        if (result != 0)
            return 20;
    }
    for(int row=0; row<15 ; row++)
    {
        for(int column=0; column<15 ; column++)
        {
            int result=checkLeftDiagonal(inputFile, row, column);
            if (result != 0)
                return 20;
        }
    }
    for(int row=0; row<15 ; row++)
    {
        for(int column=0; column<15 ; column++)
        {
            int result=checkRightDiagonal(inputFile, row, column);
            if (result != 0)
                return 20;
        }
    }
}



int main(){

fstream inputFile;
inputFile.open("wordsearch_data.txt");

initialize(inputFile);
displayConfig();

wordcheck(inputFile);

checkConfiguration(inputFile);
}
I changed it up but it still isn't returning any words found

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;
const int ROWS=15;
const int COLUMNS=15;
char table[ROWS][COLUMNS];
const int MAX=5;
int input=0;
string words[MAX];


void initialize(fstream &inputFile){

    for (int i=0; i<ROWS; i++)
    {

        for (int j=0; j<COLUMNS; j++)
            inputFile>>table[i][j];
    }

}
void displayConfig()
{
    for (int i=0; i<ROWS; i++)
    {
        for (int j=0; j < COLUMNS; j++)
        {

            cout << table[i][j];

        }
cout << endl;
    }

}
int wordcheck(fstream &inputFile){


for(int k=0; k<MAX; k++){
    inputFile>>table[15][15]>>words[k];
    cout<<words[k]<<endl;
}

    }


string checkRow(fstream &inputFile, int i)
{

    string temp = "";
    for(int j=0; j<15 ; j++){
        inputFile>>table[i][j];
        temp += table[i][j];}

    int k=1;

do{
if (temp.find(words[k]) != string::npos)
{
    return words[k];
}
else
 k++;
}
while (k<=5);

}


string checkColumn(fstream &inputFile, int j )

{
int k=1;

    string temp = "";
    for(int i=0; i<15 ; i++){
        inputFile>>table[i][j];
        temp += table[i][j];}

do{

if (temp.find(words[k]) != string::npos)
{
    return words[k];
}
else
       k++;
}
while (k<=5);
}

string checkRightDiagonal(fstream &inputFile, int i, int j )
{
int k=1;
    string temp = "";

    while (i<15 && j<15)
    {   inputFile>>table[i][j];
        temp += table[i][j];
        i++;
        j++;

    }
do{

if (temp.find(words[k]) != string::npos)
{
    return words[k];
}
else
    k++;
}
while (k<=5);
}

string checkLeftDiagonal(fstream &inputFile, int i , int j)
{

   int k=1;
    string temp = "";

    while (i<15 && j>=0)
    {
        inputFile>>table[i][j];
        temp += table[i][j];
        i++;
        j--;
    }
do{

if (temp.find(words[k]) != string::npos)
{
    return words[k];
}
else
    k++;
}
while (k<=5);
}


int checkConfiguration(fstream &inputFile)
{

    for(int row=0; row<15 ; row++)
    {
        string result = checkRow(inputFile, row);

        {
            cout<< result;
        }
    }
    for(int column=0; column<15 ; column++)
    {
        string result = checkColumn(inputFile, column);

            cout << result;
    }
    for(int row=0; row<15 ; row++)
    {
        for(int column=0; column<15 ; column++)
        {
            string result=checkLeftDiagonal(inputFile, row, column);

                cout<< result;
        }
    }
    for(int row=0; row<15 ; row++)
    {
        for(int column=0; column<15 ; column++)
        {
            string result=checkRightDiagonal(inputFile, row, column);

                cout<<result;
        }
    }
}



int main(){

fstream inputFile;
inputFile.open("wordsearch_data.txt");

initialize(inputFile);
displayConfig();

wordcheck(inputFile);

checkConfiguration(inputFile);
}
Topic archived. No new replies allowed.