Reputation: 31
My program is complete the only errors I'm getting are from my binary search and my linear search. They are the same error so I'm only going to post one.
bool searchByDescriptionBinary(char desc[][DESC_SIZE],
const int NUM_ROWS,
char searchValue[DESC_SIZE],
int& foundPosition)
{
int first = 0,
last = NUM_ROWS - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (desc[middle] == searchValue)
{
found = true;
position = middle;
}
else if (desc[middle] > searchValue)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Upvotes: 2
Views: 190
Reputation: 320719
Firstly, it is highly unlikely that you are getting the error you are saying you are getting. The source of the issue is the fact that you attempt to return position
, where position
is an int
, from a function declared to return bool
. What are you trying to do by this? Why are you trying to return an int
from a bool
function?
In any case, this is not illegal, meaning that int
will be implicitly converted to bool
in accordance with boolean conversion rules of C++ language. Some compilers will issue a warning in such cases, but not an error. In any case, most certainly this is not what you wanted.
Secondly, from your function declaration it is pretty obvious that you intended to return the index through the foundPosition
parameter (declared as an int &
). The return
statement at the end of the function was apparently supposed to do return found
, not return position
. Yet in your code you completely ignore the existence of foundPosition
. Why? Why are you trying to return
the position instead of sending it out through foundPosition
parameter, as was originally intended? Is this your code?
Thirdly (as @Blastfurnace already noted in comments), your ==
and >
comparisons are not doing what you think they are doing. You cannot compare C-style strings using the built-in comparison operators. You have to use strcmp
instead.
Upvotes: 0
Reputation: 125749
Your function is defined as returning bool
, but you're trying to return position
, which is declared as int
.
You want to return found
(which will be true
or false
), and if found == true
set foundPosition = position
.
Upvotes: 1
Reputation: 13955
Your function is defined with bool
as the return type, but it returns an int
(in the form of the return position
statement at the end). This is a type error. Change one type or the other until they agree, and it should compile.
Upvotes: 3