+ 1
Error in Word Search Grid program
I have to find the word in a dynamic 2d array grid. First it will take the input from user if the word matches in dictionary file then it will search the word in array to find it locations. i have done most of the things but can't figure out why program is crashing. The problem is in board_search function i guess. My code is below https://code.sololearn.com/ce4SWZn65rM3/?ref=app
7 Respuestas
+ 2
~ swim ~ its 0 because i am auto growing the number of rows throw file you can see the row grow function and also char[20] is only to take name from user
+ 1
I'm afraid to mention that the program oozing memory! After Line 79 you'd place two blocks of deallocation like
for (int i = 0; i< number_of_row; ++i)
delete [] board[i];
delete [] board;
for (int i = 0; i< number_of_row; ++i)
delete [] dictionary[i];
delete [] dictionary;
to dismiss the utilized resources.
Also, an infinite loop will take place as the result of the following query
cout << endl << "Try again or -1 to exit: ";
cin >> exit;
because '\n' character after hitting the Enter, remains in the stream and next iterations can't consume it or discard it. It must be eliminated explicitly by
1) resetting the failed istream flag
2) flushing the istream from remainings
in other words, putting these lines right after while loop (line 56)
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
+ 1
To Seek Glory in Battle is Glorious its kind of working but on running program multiple times its start crashing do you think i should delete memory anywhere else?
Also i have removed that infinite loop
+ 1
If you're not sure about the existence of "board.txt" and "dictionary.txt" or perhaps somehow the opening process of them has been failed, then make sure you catch it by checking the `failbit` flag after each `fin.open()`.
if (fin.fail()) {
cout << "File does not exist!\n";
}
+ 1
To Seek Glory in Battle is Glorious bro the problem is with board_search function. It crashes after when it reaches that function. Its for university project if you could help me it would be really great. I have made some changes to the code. Here you can see it
https://code.sololearn.com/ce4SWZn65rM3/?ref=app
+ 1
Line 94 causes a major problem there. It literally blows the hell out of the program!
You mustn't delete the passed pointer to pointer of char (a.k.a `board`) in the middle of the road. I even suspect the `row_grow()` done more harm than good, and you might even consider restructuring the implementation of `row_grow()`.
+ 1
Please think about this unit.
char **row_grow(char **arr, int &rows)
{
char **temp = new char*[rows+1];
for(int i = 0; i < rows; i++)
temp[i] = arr[i];
delete []arr;
arr = 0;
arr = temp;
rows += 1;
return arr;
}
There are more than just `delete [] arr` issue. Something is really off about it.