比较字符总是返回的

comparing character always returns true

本文关键字:返回 字符 比较      更新时间:2023-10-16

为什么 hasParenthesis总是评估为true?

bool hasParenthesis = false ;
for(int i = 0; i < 255 && statement[i] != ';'; i++)
{
  if(statement[i] == '(' || statement[i] == ')')
  {
    hasParenthesis = true;
    break;
  }
}

服务员,我的循环中有一个!

假设statementstd::string,您可以两者都摆脱:

auto pos = statement.find_first_of(";()");
bool hasParenthesis = (pos != std::string::npos) && (statement[pos] != ';');

for循环启动时,将hasParenthesis设置为false。有了您目前的真实,一旦布尔值的真实,当循环重新征服时,它将始终是正确的。因此,从布尔false开始for循环逻辑。

这是一个简化的骨架:

bool hasParenthesis;
for(){
    hasParenthesis = false;
    if(){
      hasParenthesis = true;
    }
}