比较字符串C++末尾的字符

Mik*_*e55 1 c++ unix

该程序应该找到在Unix上输入的以".exe"结尾的命令行参数.由于某种原因,它不起作用.这是代码:

 int main( int argc, char* argv[] )
{
    for ( int i = 1; i < argc; i++)
    if( findExe( argv[i] ) )
      cout << argv[i] << endl;

  return 0;
}
bool findExe( char* argument )
{
  if ( strlen( argument ) >= 4 )
    {
      string testExe = ".exe";
      string initialWord=argument; //converts c-string to string
      string temp( initialWord,( initialWord.size() - 4 ),4 );//creates temp with last four characters from initialWord

      if ( !temp.compare(testExe) )
        return true;
    }
  else
    return false;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*sop 6

删除else,我想(虽然我没有编译代码来检查).在长度至少为4但字符串比较返回非零的情况下,您将到达函数的末尾而不返回.你的编译器应该警告你:打开更多警告.