I have a function where I must try to find a string in a 2D array of strings and obtain the row and column which I found the string. My code is as follows, but I get an error when I try to run it (not compile, run.) I can't figure out the error, help please?
void findItem (int &row, int &column, string arr[20][40], string item)
{ /*function: findItem
purpose: attempt to find an item and pass by parameter the row and column of the item if found. If not found, passes -1,-1
Variables are self-explanatory.
*/
row = -1; column = -1;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 40; j++)
{
if (arr[i][j] == item) //successfully runs
{
try{row = i;column = j;
throw row;
}
catch(int i)
{cout<< "Error at row "<< i<<endl;} //error is caught at the row of the string
}
}
}
}
What kind of error? Do you mean the error message that you print on line 21? Well, of course that message will get printed for each position in the 2D array where you find a string that is equal to item.
The catch block gets executed whenever there is an exception of a matching type thrown from inside the try block. You are always throwing an exception on line 18 so the error message on line 21 will always get executed. It doesn't explain why it is freezing though.