Mar*_*edy -1 c++ string compare
string Foo(string letter)
{
for (int j = 0; j < (int)alphabet.length(); j++)
{
if (letter[0] == (alphabet[j]));
return "SUCCESS";
}
return "FAILURE";
}
alphabet = "Test";
cout << Foo("f") << endl;
Run Code Online (Sandbox Code Playgroud)
这打印SUCCESS甚至认为它不应该.我的比较运算符出了什么问题?
if (letter[0] == (alphabet[j])); // Note the semicolon at the end
Run Code Online (Sandbox Code Playgroud)
你if马上跟着一个空return "SUCCESS";
删除该分号:
if (letter[0] == alphabet[j])
return "SUCCESS";
Run Code Online (Sandbox Code Playgroud)