1

I'm getting an error that I can't use the relational operator "==" to test for a string to string match. Is there a different operator needed because of the array (of strings)?

int searchArray(string name, string &firstNameArray); // declares the function

int main()
{
    string firstNameArray[7] = { "Jim", "Tuyet", "Ann", "Roberto", "Crystal", "Valla", "Mathilda" }; //declares and intializes the array

    string name = ""; 
    cout << "What's your name?"; 
    getline(cin, name);

    searchArray(name, firstNameArray[7]); // using the function


    return 0;
}

int searchArray(string name, string &firstNameArray) { //defining the function


    int position = 0; //declaring and intializing the return variable - positions 0 thru 6 for array elements and position 7 for not in array

    for (int i = 0; i < 7; i++) { //looping through the array

        if (firstNameArray[i] == name) //**error code "no operator "==" matches these operands
        {
            position == firstNameArray[i];
        }
        else
        {
            position == 7;
        }

    }

    return position;

}
user207421
  • 305,947
  • 44
  • 307
  • 483
mindBlown
  • 11
  • 2
  • Yes. I welcome all advice. – mindBlown Apr 30 '17 at 02:21
  • firstNameArray, at first, is an array of strings. In your function, it is a single string. I think you should rename your function parameter to "firstName" instead of "firstNameArray", and then treat it as a string instead of an array of strings, because you're only passing in a string, not the whole array. – JGroven Apr 30 '17 at 02:24
  • Terminology please. `==` is an operator, not an operand. `string` is the operand. – user207421 Apr 30 '17 at 04:38
  • @EJP `string` is the type of the operand – M.M Apr 30 '17 at 04:57
  • Thanks for the corrections and suggestions. @Shadi, special thanks for keeping it simple and providing the explanations. – mindBlown Apr 30 '17 at 14:52

3 Answers3

1

Consider these Notes:

  • No need for & in the function prototype as a pointer to the first element in the array will be passed
  • You should assign the function to a variable to save the returned value: ex: pos = searchArray(name, firstNameArray);
  • No [] needed in the function call.
  • You have to add output for the search result.
  • Again no need for the reference in the array parameter, i.e. No & needed.
  • in position == firstNameArray[i]; you should use = not ==.
  • You should initialize position with a value other than 0 to ARR_SIZE.
  • There should not be else statement inside searchArray.

compare your code to this working code:

#include <iostream>
using namespace std;

int searchArray(string , string [], int);

int main()
{
    const int ARR_SIZE = 7;

    string firstNameArray[ARR_SIZE] = { "Jim", "Tuyet", "Ann", "Roberto",
                                 "Crystal", "Valla", "Mathilda" };
    string name = "";
    cout << "\n What's your name? ";
    getline(cin, name);

    int pos = searchArray(name, firstNameArray, ARR_SIZE);

    if (pos == -1)
        cout << "\n Not Found!";
    else
        cout << "\n Fount at position " << pos;

    cout << "\n\n\n";

    return 0;
}

int searchArray(string name, string fNameArray[],const int SIZE) {

    int position = -1;

    for (int i = 0; i < SIZE; i++)
        if (fNameArray[i] == name)
            position = i;

    return position;
}
Shadi
  • 1,701
  • 2
  • 14
  • 27
  • 1
    You don't need to specify the array size in the parameter, but you should add *another* parameter giving its size, rather than sprinking `7` everywhere. – user207421 Apr 30 '17 at 04:38
  • @EJP: You are right, I just wanted it to be simple and keep the code near to the code in the question as much as possible. – Shadi Apr 30 '17 at 04:40
  • 1
    "arrays are passed by reference by default" No they're not. They cant be passed by value, but if you want to pass by reference you need to specify it yourself (and the syntax is complicated.) – juanchopanza Apr 30 '17 at 07:15
  • @juanchopanza: I meant by pointer. thanks for the correction. – Shadi Apr 30 '17 at 13:15
1

Corrections/ Suggestions - more details on the comments:

    //const references
    //use of vector
    //use of vector size variable

    int searchArray(const string & name, const vector<string> & firstNameArray) { 
            int position = -1; //declaring and intializing the return variable - positions 0 thru 6 for array elements and position 7 for not in array

            for (int i = 0; i < firstNameArray.size(); i++) { //looping through the array
                //== is for comparison
                if (firstNameArray[i] == name)
                {
                    position = i; //= is for assignment
                    break; // without break always returns not found
                }
            }
            return position;
        }

        int main(int argc,const char * argv[]) {

            vector<string> firstNameArray = { "Jim", "Tuyet", "Ann", "Roberto", "Crystal", "Valla", "Mathilda" }; //declares and intializes the array

            string name ;
            cout << "What's your name?";

            getline(cin, name);

            cout << "position: " << searchArray(name, firstNameArray) << endl; 


            return 0;
        }
didiz
  • 1,069
  • 13
  • 26
0

In searchArray(name, firstNameArray[7]);, you just pass one string, not a string array. And in int searchArray(string name, string &firstNameArray), the syntax of passing array by reference is not right.

I changed a few places, hope this will help

int searchArray(string name, string* firstNameArray); // declares the function

int main()
{
    string firstNameArray[7] = { "Jim", "Tuyet", "Ann", "Roberto", "Crystal", "Valla", "Mathilda" }; //declares and intializes the array

    string name = ""; 
    cout << "What's your name?"; 
    getline(cin, name);

    searchArray(name, firstNameArray); // right
    //searchArray(name, firstNameArray[7]); 
    // this will pass a single string, not a string array, the index is out of boundary, the range is 0-6


    return 0;
}

int searchArray(string name, string* firstNameArray) { //pass a pointer
//or int searchArray(string name, string (&firstNameArray)[7])

    int position = 0; //declaring and intializing the return variable - positions 0 thru 6 for array elements and position 7 for not in array

    for (int i = 0; i < 7; i++) { //looping through the array

        if (firstNameArray[i] == name) 
        {
            position = i; //not position == firstNameArray[i]; incompatible type, int and string.
        }
        else
        {
            position = 7;
        }

    }

    return position;

}

Reference

Passing an array by reference

Community
  • 1
  • 1
Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25