[英]If statement evaluating to false even when the condition is true
即使計數器是 4,我的程序也會跳過 if 語句counter==4
當我嘗試調試時,它永遠不會檢查這個 if 語句,它每次都完全跳過它。
int num;
int counter = 1;
while (file >> num) {
if (counter == 1) {
//code
}
if (counter == 2) {
//code
}
if (counter == 2) {
//code
}
if (counter == 4) { //completely skips this if
counter == 1;
}
counter++;
}
你有這個作品:
if (counter == 4) { //completely skips this if
counter == 1;
}
它不會被跳過(從您發布的代碼中可以看出)。 它只是什么都不做。 您將counter
與 1進行比較,然后丟棄生成的布爾值。
要修復,切換到賦值,使用counter = 1
。
您的編譯器應該警告您“代碼無效”。 打開警告( -Wall -Wextra
用於gcc和clang , /W3
或/W4
用於 Visual Studio),並修復它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.