【问题标题】:C++ compare two strings bad codeC++比较两个字符串错误代码
【发布时间】:2015-11-22 12:46:38
【问题描述】:

我的代码有问题.. 我不知道出了什么问题.. 我需要比较两个字符串。错误信息: 错误:'outputFile' 没有命名类型。

我正在处理文件.. 在其中写入一些文本...我的比较代码:

for (int i = 0; i < size; i++){
            if (PorovnaniKategorie(ArrayOfWorlds[i],"mzda")==true){
//do some code
            }
            }
        }

功能:

bool PorovnaniKategorie(string s1, string s2){
    bool porovnani1=true;
    int vel1 = s1.size();
    int vel2 = s2.size();
if (vel1 == vel2)
                    {
                    for ( int i = 0; i < vel1; i++)
                    {
                        if(tolower(s1[i])!= tolower(s2[i]))
                           porovnani1 = false;
                          }
                    }
else{
    porovnani1 = false;
}
return porovnani1;
}

它是单独工作的,当我从主函数中删除循环时,一切正常..我不知道如何解决这个问题..

事实上,我只需要比较索引处的值是否与“mzda”进行比较

编辑:

outputFile << "</body>" << endl;  //this line is error
        outputFile << "</html>" << endl;
        outputFile.close();

我认为这里没有错误..(此代码在 for 下)。当我删除所有工作时

【问题讨论】:

  • 您显示的代码真的是导致错误的代码吗?因为您显示的代码中没有outputFile。如果你得到构建错误,编译器将包含文件名错误所在的行号,有时甚至是函数名来帮助你。
  • 您的比较代码(for 循环)的结束大括号比开始大括号多,尽管缩进关闭时有点难以看到。
  • 如何声明 outputFile?

标签: c++ compare


【解决方案1】:

正确的。请在 google 中询问“std string compare ignore case”。

bool PorovnaniKategorie( string s1, string s2 )
{
    if( s1.size() == s2.size() ) {
        for( std::string::size_type i = 0; i < s1.size(); i++ ) {
            if( tolower( s1[i] ) != tolower( s2[i] ) ) {
                return false;
            }
        }
        return true;
    }
    return false;
}

【讨论】:

  • 请给出完整的例子,什么是outputFile。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2015-11-13
相关资源
最近更新 更多