Hope I'm posting this in the correct forum..

Ok... student here... so I'm not looking for an answer... nearly some guidance... Sorry if I get the posting of it incorrect I'm just trying to find some help. And now my code blocker isn't working... so I can't exactly remember what the error code was but it has something to do with the string array. When I was running it, it seemed to try and change my key from a string to a character... sorry I can't get it exactly. I will try and fix my code blocker and post something else.

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
#include <iostream>
#include <string>
using namespace std;


template<typename T>
T binarySearch( const T list[], T key, int arraySize)
{
    int low = 0;
    int high = arraySize - 1;

    while (high >= low)
    {
        int mid = (low + high) / 2;
        if (key < list[mid])
            high = mid -1;
        else if (key == list[mid])
            return mid;
        else
            low = mid + 1;
    }
    return -1;
}

int main()
{

    int list1[] = {1,8, 15, 25, 33, 100, 200};
    cout << "The location of the number you are searching for is  " << binarySearch( list1, 15, 7) +1 << " on the list." << endl;

    double list2[] = {.125, 1.5, 9.8, 25.68, 98.44};
    cout << "The location of the number you are searching for is  " << binarySearch( list2, 98.44, 5) +1 << " on the list." << endl;

    char list3[] = {'A', 'B', 'D', 'H', 'L', 'M', 'R', 'S', 'T'};
    cout << "The location of the letter you are searching for is  " << binarySearch( list3, 'M', 9) +1 << " on the list." << endl;

    string list4[] = {"So", "Im", "getting", "hungry"};
    cout << "The location of the word you are searching for is  " << binarySearch( list4, "hungry", 4) +1 << " on the list." << endl;

    return 0;
}
Change line 38: "hungry" -> string("hungry"). It needs to be the same type. Or you define a second template parameter.

Line 7: The return type needs to be int.
The change to line 38 does not work. And on line 7, I am using template type so I don't want to be specific with that code.
Last edited on
The change to line 38 worked for me. The implicit type of "hungry" is const char *, not string so it does not match the template. Note that fixing line 38 without fixing line 7 won't work.

And on line 7, I am using template type so I don't want to be specific with that code.
coder777 is correct. The return the of the function should be int, not T. Line 18 - You are returning the index (mid) which is an int, not an element of type T.
Last edited on
Ok I changed it and it works. But I have a question... why does it work for the other three and not the string. If you comment out the string it works fine. What is different about strings?
Last edited on
The other three are all built-in types (int, double, char). The template recognizes those with no issue.

The problem arises as I stated in my previous post, because "hungry" in implicitly type const char *, not string.
Topic archived. No new replies allowed.