2015-12-17 21 views
0

這條if語句不起作用。Trying If語句

任何人都可以告訴爲什麼下面的代碼不起作用,並幫我修復?

i=0; 

while(1) 
{ 
    if(counter2 < storeanother[0].size()) 
     break; 

int j=0; 

    while(1) 
{ 
    if(j < handler) 
     break; 

    outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl; 
    counter2+=1; 

     j++; 
} 
i++; 
} 
    outputFile2.close(); 
+1

你想做什麼?現在的結果是什麼?最重要的是,在第一個For循環中,爲什​​麼要用'counter2'而不是'i'進行測試?在第二部分中,爲什麼使用while循環而不是For循環? – DrDonut

+0

...以及所有的「代碼」如何與C++ 11相關? – zaufi

+0

你正在做i ++和j ++的時間太晚了。這是一個後期增量,可以在您打開while循環時儘早完成 –

回答

1

你打破了錯誤的條件,你必須扭轉它。例如:

if(j == handler) 
    break; 
1

因爲在for循環,當條件得到真,那麼只有循環運行 ,在這裏你所做的條件真在這兩種情況下,以便它只是從環打破
改變它的對面,它將正常工作。
像這樣:

i=0; 
// for (int i = 0; counter2 < storeanother[0].size(); i++) 
while(1) 
{ 
    if(counter2 >= storeanother[0].size()) 
     break; 

int j=0; 
//for (j = 0; j < handler; j++) 
    while(1) 
{ 
    if(j >= handler) 
     break; 

    outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl; 
    counter2+=1; 

     j++; 
} 
i++; 
} 
    outputFile2.close(); 
2

您反轉條件

for (init; cond, post) { body;} 

相當於

init; 
while (true) { 
    if (!cond) { // and not simply cond 
     break; 
    } 
    body; 
    post; 
} 

甚至

init; 
while (cond) { 
    body; 
    post; 
} 

所以

if(counter2 < storeanother[0].size()) 
    break; 

應該

if (counter2 >= storeanother[0].size()) 
    break; 

,併爲其他類似的循環。

0

再說如果由其他人所說的條件是錯誤的,我看不出有任何理由在這裏有

while(1) 
{ 
    if(j >= handler) 
     break; 
    // do the stuff 

,而不是

while(j < handler) 
{ 
    // do the stuff 
0

我的猜測是,你正在嘗試做的這樣的:

int i=0; 
while(counter2 < storeanother[0].size()) 
{ 

    for (int j = 0; j < handler; j++) 
    { 
     outputFile2 << storeanother[0][counter2] << " + " <<streanother[1][counter2] << " = " << z80[i].get(j) << endl; 
     counter2+=1; 
    } 
    i++; 
} 
outputFile2.close(); 

而對於第二部分:

i=0; 
// for (int i = 0; counter2 < storeanother[0].size(); i++) 
while(counter2 < storeanother[0].size())) 
{ 

int j=0; 
//for (j = 0; j < handler; j++) 
    while(j < handler) 
{ 
    outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl; 
    counter2+=1; 

     j++; 
} 
i++; 
} 
    outputFile2.close();