以下是我遇到问题的一段代码的极简化版本。可能由if语句导致的C++布尔逻辑错误
int i = 0;
int count = 0;
int time = 50;
int steps = 1000;
double Tol = 0.1;
bool crossRes = false;
bool doNext = true;
for (int i=0; i<steps; i++) {
//a lot of operations are done here, I will leave out the details, the only
//important things are that "dif" is calculated each time and doNext either
//stays true or is switched to false
if (doNext = true) {
if (dif <= Tol) count++;
if (count >= time) {
i = steps+1;
crossRes = true;
}
}
}
if (crossRes = true) {
printf("Nothing in this loop should happen if dif is always > Tol
because count should never increment in that case, right?");
}
我的问题是,每次它被用在for循环完成时,它执行里面的“如果(crossRes =真)”括号即使计数没有被增加的语句。
其== == true。 =分配一个值而不是比较它 – 2012-07-30 21:23:55
请不要在您的问题中编辑更正的代码。这使得不可能看到问题是什么,并导致答案没有意义。我要恢复你的编辑。 – 2012-07-30 21:30:04