Linear Search Function

I am trying to make all the characters in an array uppercase, however it is giving me an error that says:
cannot convert std :: (aka std::basic string<char>)' to 'const char' for argument 1 to 'size_t strlen(const char*)
This error happens on line 10 and I do not know what the problem is. Any help would be greatly appreciated thanks.

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
void LinearSearch(string array[], int num_Elems, char data[], int plyr[], float ppg[])
{
    char *strPtr = nullptr;
    int index = 0;
    string this_name;
    int num_chars;

    for(int i = 0; i < num_Elems; i++)
    {
        num_chars = strlen(array[i]);
        this_name = array[i];
        for (int j = 0; j < num_chars; j++)
            {
            this_name[j] = toupper(this_name[j]);
            }
    }


   for (index = 0; index < num_Elems; index++)
   {
      strPtr = strstr(array[index], data);
      if (strPtr != nullptr)
            break;
   }

   if (strPtr != nullptr)
        cout << array[index] << endl << plyr[index] << " " << ppg[index] << endl;
   else
        cout << "No player could be found.\n";


}
Last edited on
line 1: You're passing an array of std::string objects.

line 10: You can't use strlen() directly on a std::string object. strlen() is a C function that takes a const char *. To take the length of a std::string, use string.size().
 
num_chars = array[i].size();


You could use std::string's c_str() function to return a const char *, but that's mixing C and C++ idioms which is a poor practice.
Topic archived. No new replies allowed.