2013-03-24 56 views
0

幾乎標題所說的內容。我到了這個彩票號碼分配的最後一位,我不確定爲什麼第二組if/if語句在調試時不顯示。我知道if/else語句是相互排斥的 - 但是如果兩者都測試,不應該是if和then呢? 這是代碼。If/else如果不執行,即使在while循環內出現條件

count=0; 
while(count<5) 
{ 
    if(lottery[count] == user[count]) 
    { 
     lotto = lottery[count]; 
     cout<<"So, you matched numbers with "<<lotto <<".\n"; 
     tally++; 
    } 
     if(tally==5 && count == 5) 
     { 
      cout<<"Congratulations, you're the grand prize winner!"; 
     } 
     else if(tally < 5 && count == 5) 
     { 
      cout<<"A total of "<<tally<<" of your lottery picks matched."; 
     } 
     else if(tally == 0 && count == 5) 
     { 
      cout<<"Caution. The following comment is an inside joke. Read at your own risk."; 
      cout<<"Bet you feel like a total loser huh? Nothing matched."; 
     } 

    count++; 
} 

我知道我可能應該用for循環代替while循環,但爲了簡單起見,我更加舒服。

+0

你是否在調試器中逐行執行代碼? – 2013-03-24 17:04:40

回答

8

count從不5if塊被執行時。

只要它變成5,條件就會失敗,循環停止。

+0

謝謝!這是一個愚蠢的錯誤! – 2013-03-24 17:04:12

5

count將永遠不會等於5在while循環內的條件需要它的點。如果您在之前遞增了count,if-else那麼就可以滿足其中一個條件(取決於tally的值)。

+0

脫衣服......請閱讀本章的內容。 D'哦。謝謝! – 2013-03-24 17:05:10

+0

這不是事實。 while循環後'count'將等於5。 – Omnifarious 2013-03-24 17:06:15

+0

@Omnifarious你是對的。我的措辭全錯了。 – juanchopanza 2013-03-24 17:08:20