2013-10-17 86 views
0
if (ch1 = 'l' || 'L') 
     cout << left; 
    else if (ch1 = 'r' || 'R') 
     cout << right; 
    else 
     cout << "error" << endl; 

    cout << setw(++aw) << setfill(char(a)) << s1 << endl; 

    if (ch2 = 'l' || 'L') 
     cout << left; 
    else if (ch2 = 'r' || 'R') 
     cout << right; 
    else 
     cout << "error" << endl; 

    cout << setw(++bw) << setfill(char(b)) << s2 << endl; 

    if (ch3 = 'l' || 'L') 
     cout << left; 
    else if (ch3 = 'r' || 'R') 
     cout << right; 
    else 
     cout << "error" << endl; 

    cout << setw(++cw) << setfill(char(c)) << s3 << endl; 

return 0; 
} 

我不完全確定爲什麼,但所有3個輸出行出來左對齊。如果有一個邏輯錯誤的地方,似乎合法的我,我還不能肯定還是我剛纔輸入它錯理由不能正常工作

+0

'then'不是C++關鍵字... –

+0

你需要學習一些基本的C++,包括什麼是*文字*值,像字符文字''L''。另外,如何在AND或OR鏈中使用多個條件。 –

+0

我們可以看看'ch1','left'和'right'的聲明嗎? – scones

回答

0

當你編寫例如

if (ch1 = 'l' || 'L') 

這將永遠是正確的,因爲你實際上是說

assign 'l' to ch1 (whose result is always is != 0) 

OR

if 'L' is not equal to zero (which always is !=0) 

,而不是把它寫正確的方法是使用雙=

if (ch1 == 'l' || ch1 == 'L') 

if (toupper(ch1) == 'L') 
1

的比較應該是

if (ch1 == 'l' || ch1 == 'L') 
    ch1 = left; 
else if(ch1 == 'r' || ch1 == 'R') 
    ch1 = right; 
else 
    cout << "error" << endl; 

「L」,「R」,「R ','L'應該是字符文字,除非你有變量l,r,L,R和適當的字符。