can't find logic error in program.

Find a word game, we have a table of characters, a size of the table, and a word to find in every direction of the table.e.g.
1
2
3
4
5
6
7
8
9
10
11
12
size=4,4
searchFor=and
dnal
mnwa
dvan
ryqd

Solved:
dna.
.n.a
d.an
...d

This is a lot of code, the flow of it goes it gets lines from input file, sets booleans: gotSize, gotWord, gotPuzzle, and createdArray. when all have been achieved will send the puzzle to function for processing, then output the result. My problem is it is the solved puzzle is not being written to line 369..
A lot of the functions can be ignored, the primary focus is lines 272 to 378. The output I'm getting is the correct size, word, puzzle input, and then the solved puzzle is simply printing out, but it does not contain the solved words it only is filled in with the '.' dots as I instructed on line 130.
So I would like you to look at the algorithm for finding the words n, ne, nw, s, se, sw, w, e, and see if there is something I'm missing... to me it seems like everything should work but it's not happening :(
output for the above example:
1
2
3
4
5
6
7
8
9
10
11
12
size=4,4
searchFor=and
dnal
mnwa
dvan
ryqd

Solved:
....
....
....
....

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
195
196
197
#include "wordfinder.h"
#include "array2d.h"

using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::exit;

const string gc_usage="Usage: a1 -f input_file";

WordFinder::~WordFinder(void)
{
    delete[] arr_word;
}

string WordFinder::checkArgs(int argc, char **argv)
{
    int row=1;
    string sw="", infl="";

    while (row < argc)
    {
        sw = argv[row];
        if (sw == "-f")
        {
            row++;
            if (row < argc)
                infl = argv[row];
        }
        else
        {
            cout << "Unknown Switch:[ " << sw << " ]\n";
            cout << gc_usage << endl;
            exit(1);
        }
        row++; // incase more instances of '-f' occur
    }
    if (infl.length() < 1)
    {
        cout << "Error: must enter a filename.\n";
        cout << gc_usage << endl;
        exit(1);
    }
    return infl;
}

void WordFinder::parseFile(string infile)
{
    string line="", tmp_line="", word="";
    string size="SIZE=";
    string search="SEARCHFOR=";
    int row=0, col=0;
    int line_count=0, puz_line_cnt=0;
    Array2D puz_arr;
    Array2D solved_puz;
    resetBools();

    ifstream ifs(infile.c_str());
    if (! ifs.is_open())
    {
        cout << "Error: error opening file: " << infile << "\n";
        cout << gc_usage << endl;
        exit(1);
    }
    // else ->

    while (! ifs.eof())
    {
        getline(ifs,line);
        ++line_count;
        tmp_line = line;
        convert2Upper(tmp_line);

        if (isComment(line))
        {
            //== will  be ignored ==//
        }
        else if (isSize(tmp_line))
        {
            if (gotSize)
            {
                cout << "Error: " << line_count << " : already have size."
                        << endl;
                cout << "Reset and getting next puzzle." << endl;
                resetBools();
            }
            gotSize = parseSize(line,row,col);
        }
        else if (isSearch(tmp_line))
        {
            if (gotWord)
            {
                cout << "Error: " << line_count
                        << " : already have search string." << endl;
                cout << "Reset and getting next puzzle." << endl;
                resetBools();
            }
            gotWord = parseWord(line,word);
            if (gotSize && gotWord && gotPuzzle)
            {
                // send puzzle to solved function
                processPuzzle(puz_arr,solved_puz,word,row,col);
                cout << "Size=" << row << "," << col << endl;
                cout << "SearchFor=" << word << endl;
                for (int i=0; i<row; i++)
                {
                    for (int k=0; k<col; k++)
                        cout << puz_arr(i,k);
                    cout << endl;
                }
                outputSolvedPuzzle(solved_puz,row,col);
                resetBools();
            }
        }
        else // line is a puzzle.
        {
            if (gotSize)
            {
                if (! createdArr)
                {
                    Array2D new_puz(row,col);
                    Array2D new_solved(row,col);
                    createdArr = true;
                    // set solved to dots.
                    for (int i=0; i<row; i++)
                        for (int j=0; j<col; j++)
                            new_solved(i,j) = '.';
                    
                    puz_arr = new_puz;
                    solved_puz = new_solved;
                } 

                size_t found;
                string tmp_puz_line;
                found=line.find_first_of(" .#\n");
                tmp_puz_line = line.substr(0,found);

                if (tmp_puz_line.size() == (unsigned)col)
                {
                    if (puz_line_cnt<row)
                    {
                        for (int i=0; i<col; i++)
                        {
                            puz_arr(puz_line_cnt, i) = tmp_puz_line[i];
                        }
                        ++puz_line_cnt;
                        if (puz_line_cnt == row) // last line of puzzle?
                        {
                            puz_line_cnt=0;
                            gotPuzzle=true;
                        }
                    }
                    else
                    {
                        cout << "Error: " << line_count
                                << " too much puzzle to process" << endl;
                    }

                    if (gotSize && gotWord && gotPuzzle)
                    {
                        processPuzzle(puz_arr,solved_puz,word,row,col);
                        // output the input and result
                        cout << "Size=" << row << "," << col << endl;
                        cout << "SearchFor=" << word << endl;
                        for (int i=0; i<row; i++)
                        {
                            for (int k=0; k<col; k++)
                                cout << puz_arr(i,k);
                            cout << endl;
                        }
                        outputSolvedPuzzle(solved_puz,row,col);
                        resetBools();
                    }
                }
                else
                {
                    cout << "Error: " << line_count
                            << " : puzzle size differs from input size.\n"
                            << "Reset and getting next puzzle." << endl;
                    resetBools();
                }

            }
            else
            {
                cout << "Error: " << line_count
                        << " : don't have size, cannot process puzzle.\n"
                        << "Reset and getting next puzzle." << endl;
                resetBools();
            }
		}
	} // end while
	ifs.close();
}
Last edited on
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
bool WordFinder::parseWord(string line, string& word)
{
    int start, end;
    start = line.find('=');
    end = line.find_first_of(" #\t\n");

    int len1 = end - start - 1;
    word = line.substr(start+1,len1);

    if (word.size()>1) return true;
    else
    {
        cout << "Word is too small to play with: \'" << word << "\'" << endl;
        return false;
    }
}

bool WordFinder::parseSize(string line, int& row, int& col)
{
    int size_pos[3]={ 0 };
    string strRow, strCol;

    size_pos[0] = line.find('='); // indexes
    size_pos[1] = line.find(',');
    size_pos[2] = line.find_first_of(" #\t\n");

    int len1 = size_pos[1] - size_pos[0] - 1;
    strRow = line.substr(size_pos[0]+1,len1);
    int len2 = size_pos[2] - size_pos[1] - 1;
    strCol = line.substr(size_pos[1]+1,len2);
    //*
    //*/

    if (gotInteger(strRow,row))
    {
        if (gotInteger(strCol,col))
        {
            if (row > 1 && col > 1)
            {
                return true;
            }
            else
            {
                cout << "Puzzle size is too small to play with: "
                        << row << " * " << col << endl;
                return false; // row or col < 2
            }
        }
        else
        {
            cout << "Cannot parse Column number." << endl;
            return false; // col is not an int
        }
    }
    else
    {
        cout << "Cannot parse Row number." << endl;
        return false; // row is not an int
    }
} // end parseSize();

void WordFinder::convert2Upper(string& str)
{
    for (unsigned i=0; i<str.length(); i++)
        str[i]=toupper(str[i]);
}

bool WordFinder::gotInteger(const string& str, int& i)
{
    istringstream ss(str);
    return ss >> i ? true : false;
}

void WordFinder::processPuzzle(Array2D& puz_arr, Array2D& solved_puz,
		string word, int row, int col)
{
    //*************************************//

    int arr_word_size = word.size();
    arr_word = new char[arr_word_size];
    for (unsigned i=0; i<word.size(); i++)
        arr_word[i] = word[i];
    //**** no std::string or cstring from here on ****//
    int north = -1;
    int south = 1;
    int west = -1;
    int east = 1;
    int straight =0;

    for (int j=0; j<row; j++) 
    {
        for (int k=0; k<col; k++) 
        {
            // if element == word[0]
            // then search in every direction
            // n(-1,0), ne(-1,-1), e(0,1), se(1,1),
            // s(1,0), sw(1,-1), w(0,-1), nw(-1,-1)
            // for the rest of word
            if (arr_word[0] == puz_arr(j,k))
            {
                bool gotDir=false;
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,straight,j,k);
                if (gotDir) // NORTH
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,straight,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,east,j,k);
                if (gotDir) // NORTH-EAST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,east,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,straight,east,j,k);
                if (gotDir) // EAST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,straight,east,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,east,j,k);
                if (gotDir) // SOUTH-EAST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,east,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,straight,j,k);
                if (gotDir) // SOUTH
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,straight,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,west,j,k);
                if (gotDir) // SOUTH-WEST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,west,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,straight,west,j,k);
                if (gotDir) // WEST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,straight,west,j,k);
                }
                gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,west,j,k);
                if (gotDir) // NORTH-WEST
                {
                    setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,west,j,k);
                }
            }
        } // fk
    } // fj

}

bool WordFinder::lookDirection(Array2D& puz_arr, char arr_word[], int arr_word_size,
		int dirX, int dirY, int eleX, int eleY)
{
    int row= puz_arr.getHt();
    int col= puz_arr.getWd();
    // don't need to start at zeroth, first char is good
    // check for out of bounds, else check character.
    for (int i=1; i<arr_word_size; i++)
    {
        if ((eleX += dirX)>row || (eleX += dirX)<0)
            return false;
        else if ((eleY += dirY)>col || (eleY += dirY)<0)
            return false;
        else
        {
            if (puz_arr((eleX+=dirX),(eleY+=dirY)) != arr_word[i])
                return false;
        }
    }

    return true;
}

void WordFinder::setSolvedPuzzle(Array2D& solved_puz, char arr_word[], int arr_word_size,
		int dirX, int dirY, int eleX, int eleY)
{
    for (int i=0; i<arr_word_size; i++)
    {
        solved_puz(eleX, eleY) = arr_word[i];
        eleX += dirX;
        eleY += dirY;
    }
}

void WordFinder::outputSolvedPuzzle(Array2D& solved_puz, int row, int col)
{
    for (int i=0; i<row; i++)
    {
        for (int j=0; j<col; j++)
            cout << solved_puz(i,j);
        cout << endl;
    }
}

void WordFinder::resetBools(void)
{
    gotSize=false;
    gotWord=false;
    gotPuzzle=false;
    createdArr=false;
}

bool WordFinder::isComment(string str)
{
    if (str == "" || str[0]=='#' || str[0]=='\n')
        return true;
    else return false;
}

bool WordFinder::isSize(string str)
{
    if (str[0]=='S' && str[1]=='I' && str[2]=='Z' && str[3]=='E' && str[4]=='=')
        return true;
    else return false;
}

bool WordFinder::isSearch(string str)
{
    if (str[0]=='S' && str[1]=='E' && str[2]=='A' && str[3]=='R' &&
        str[4]=='C' && str[5]=='H' && str[6]=='F' && str[7]=='O' &&
        str[8]=='R' && str[9]=='=')
        return true;
    else return false;
}
Last edited on
woohoo I found it.... so dumb, that took 3 days! ffs I was supposed to hand it in on tues night.

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
for (int i=1; i<arr_word_size; i++)
{
   if ((eleX += dirX)>row || (eleX += dirX)<0)
      return false;
   else if ((eleY += dirY)>col || (eleY += dirY)<0)
      return false;
   else
   {
      if (puz_arr((eleX+=dirX),(eleY+=dirY)) != arr_word[i])
         return false;
      }
}

    return true;

// Should have been:
// start at zero, because that's the index i'm sending to the function
// and, multiple += for each direction doesn't work. Every case is incremented.
for (int i=0; i<arr_word_size; i++) 
{
   if (eleX>row || eleX<0)
      return false;
   else if (eleY>col || eleY<0)
      return false;
   else
   {
      if (puz_arr(eleX,eleY) != arr_word[i])
         return false;
   }
   eleX+=dirX;
   eleY+=dirY;
}

    return true;
Last edited on
How do you do line continuations?
[code firstline=666]

Last edited on
Thanks :)
wait, wth... I just thought about that again, if any of those are the case:
if ((eleX += dirX)>row || (eleX += dirX)<0)
else if ((eleY += dirY)>col || (eleY += dirY)<0)

the return should simply return false....
These are the checks for out of bounds, and
if (puz_arr((eleX+=dirX),(eleY+=dirY)) != arr_word[i])

should simply increment and test the current elements with the word... even if it was pre-increment before the condition, the initialization of 'i' should have been correct at 1, and i did test 0 as well....

so why did it not work with the previous for loop?

Last edited on
yeah yeah thanks chris :P only person who could help me and you completely ignore the thread....

lets say in the case of an 8*8 grid, we are at the first element [0][0] and checking south

if ( 0 > 8 || 0 < 0) false...
if ( 0 > 8 || 0 < 0) false...

and lets say the word is in fact south,

if arr[0][0] != word[0]; false...

i++
if ( (0 += 1) > 8 || 0 < 0) false...
if ( (0 += 0) > 8 || 0 < 0) false...

if arr[1][0] != word[1] false...
i++
if ( (1 += 1) > 8 || 0 < 0) false...
if ( (0 += 0) > 8 || 0 < 0) false...

if arr[1][0] != word[1] false...
if ( (2 += 1) > 8 || 0 < 0) false...
if ( (0 += 0) > 8 || 0 < 0) false...

if arr[1][0] != word[1] false...

and so on for the length of the word.... still seems like it should work to me and return true...

edit:
Whatever! it works now who am I to question it.
Last edited on
constant+=constant
Uh...
why are they constant?

edit: ok I got it:
1
2
3
4
5
6
7
int i=1;
while (true)
{
   if ((i+=1) > 10 || (i+=1) >50)
      break;
   cout << i << endl;
}


3
5
7
9
11

not quite sure how the 11 gets there...
i+1 > 10 ... nothing happens.
when you do math assignment inside conditional it happens.
Last edited on
Topic archived. No new replies allowed.