我的循环条件没有得到满足

时间:2013-01-04 08:19:50

标签: c++ string std

由于某种原因,下面的C ++会一直返回false。任何帮助都会被施展!我会包含更多来源,但它在LibPQ中

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one.compare( three ) == 0 && two.compare( four ) == 0 )  {

  return true;
} else {

  return false;
}

3 个答案:

答案 0 :(得分:0)

你试过了吗?

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one == three && two == four ) {

  return true;
} else {

  return false;
}

答案 1 :(得分:0)

问题是你没有{在你的if之后。试试这个

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one.compare( three ) == 0 && two.compare( four ) == 0 )
{
  return true;
} else {

  return false;
}
是的,您的代码甚至可以与之相符吗?

答案 2 :(得分:0)

您可以尝试以下内容:

#include "iostream" 

using namespace std; 

std::string one = "amit"; 
std::string two = "kumar"; 
std::string three = "amit"; 
std::string four = "kumar"; 

bool strcomp() 
{ 
    if (one.compare(three) == 0 && two.compare(four) == 0) 
    { 
        return true; 
    } 

    return false; 
} 

int main() 
{ 
    if (strcomp()) 
    { 
       cout<<"Equal"<<endl; 
    } 
    else 
    { 
       cout<<"Defferent"<<endl; 
    } 

} 

输出:等于

相关问题