我收到一个错误,我不能使用关系运算符“==”来测试字符串到字符串匹配。由于数组(字符串),是否需要不同的运算符?
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;
}
答案 0 :(得分:1)
请考虑以下注释:
&
,因为指向数组中第一个元素的指针将被传递pos = searchArray(name, firstNameArray);
[]
。&
需要。 position == firstNameArray[i];
中=
而不是==
。0
到ARR_SIZE
以外的值初始化位置。else
内不应有searchArray
声明。 将您的代码与此工作代码进行比较:
#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;
}
答案 1 :(得分:1)
更正/建议 - 有关评论的更多详情:
//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;
}
答案 2 :(得分:0)
在searchArray(name, firstNameArray[7]);
中,您只传递一个字符串,而不是字符串数组。在int searchArray(string name, string &firstNameArray)
中,通过引用传递数组的语法不正确。
我换了几个地方,希望这会有所帮助
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;
}