2

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;
 }
Chad Loos
  • 31
  • 2

4 Answers4

3

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.

Gian
  • 13,735
  • 44
  • 51
  • ty guys very much appreciated for the help – Chad Loos Dec 01 '11 at 02:01
  • 1
    @Gian: It is type inconsistency, but not an error. In C++ language it is perfectly legal to initialize a `bool` value from an `int` value, although the effect of that is certainly not what the OP intended. – AnT stands with Russia Dec 01 '11 at 02:07
  • I stand by my usage. It may be a well-typed C++ program (although the OP specified than an error was occurring, so perhaps some compiler options were enabled to treat this as an error), but this is still a 'type error', in the language-generic sense. We can talk about runtime type errors in untyped languages, for example, and we can talk about the absence of type errors in languages with certain type system properties. "This is a type error" is shorthand for "you have used values of different types incorrectly with respect to what you were trying to achieve". – Gian Dec 01 '11 at 02:11
  • @Gian: Not really. Firstly, relying on well-defined type conversion properties of the language is a perfectly good practice. Personally, I wouldn't rely on it in this case, but nevertheless it is not an error at all. Secondly, you suggest simply changing on of the types in order to make them match. In this case the proper thing to do is to change the code to make it make sense. The original code makes no sense whatsoever. Fixing "type errors" by mechanically changing the type will indeed make it compile, but the code still won't make any sense and won't do anything meaningful. – AnT stands with Russia Dec 01 '11 at 02:20
  • It's "good practice" to write programs that don't work as intended? The question was specifically about how to get it to compile. I assumed a subsequent question would come if the OP couldn't figure out how to make it *work* after succeeding in making it compile. – Gian Dec 01 '11 at 02:25
1

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
0

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

You are supposed to return found; instead of return postion;

Taha
  • 1,086
  • 1
  • 10
  • 20