다음은 문제가있는 코드 섹션의 매우 단순화 된 버전입니다.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입니다. = 값을 비교하는 대신 값을 할당합니다. –
수정 된 코드를 질문에 편집하지 마십시오. 따라서 문제가 무엇인지 파악할 수 없으며 해답을 이해하지 못하게됩니다. 편집을 되돌릴거야. –