2015-09-01 99 views
-2

做下面的代碼和編譯錯誤我不知道了要如何處理這個C​​ ++

#include <iostream> 
using namespace std; 

int main() 
{ 
double RTinSeries, RTinParallel, R1, R2, R3, R4, R5, R6, Option, Opt1, Opt2; 
cout<<"Enter your option:(1 for parallel and 2 for series)"; 
cin>>Option; 
if (Option =< 1 && Option >=3) 
{ 
    cout<<"Error!"; 
    cout<<"You've enter a wrong Option"; 
} 
{ 
    else if (Option = 1); 
    for (Opt1=RTinParallel; RTinParallel = (1/((1/R1)+(1/R2)+(1/R3)+(1/R4)+    (1/R5)+(1/R6)));); 
    cout<<"Your Choice is Parallel"; 
    cout<<"Enter Resistance Value for R1:"; 
    cin>>R1; 
    cout<<"Enter Resistance Value for R2:"; 
    cin>>R2; 
    cout<<"Enter Resistance Value for R3:"; 
    cin>>R3; 
    cout<<"Enter Resistance Value for R4:"; 
    cin>>R4; 
    cout<<"Enter Resistance Value for R5:"; 
    cin>>R5; 
    cout<<"Enter Resistance Value for R6:"; 
    cin>>R6; 
    cout<<"Total Resistance in Parallel is:"<< Opt1<<"ohms"<<endl; 
} 
{ 
else (Option = 2); 
for (Opt2=RTinSeries; RTinSeries = R1 + R2 + R3 + R4 + R5 + R6;) 
    cout<<"Your Choice is Series"; 
    cout<<"Enter Resistance Value for R1:"; 
    cin>>R1; 
    cout<<"Enter Resistance Value for R2:"; 
    cin>>R2; 
    cout<<"Enter Resistance Value for R3:"; 
    cin>>R3; 
    cout<<"Enter Resistance Value for R4:"; 
    cin>>R4; 
    cout<<"Enter Resistance Value for R5:"; 
    cin>>R5; 
    cout<<"Enter Resistance Value for R6:"; 
    cin>>R6; 
    cout<<"Total Resistance in Series is:"<<Opt2<<"ohms"<<endl; 
} 

    system("pause"); 
    return 0; 


} 

而且編譯的結果

9:15: error: expected primary-expression before '<' token 
15:7: error: 'else' without a previous 'if' 
15:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
16:92: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
33:3: error: 'else' without a previous 'if' 
34:63: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 
+0

我建議讀或在C或C++的基礎知識的基礎課程。你有基本的語法錯誤,你應該學習如何自行修復。 –

+0

'else if(Option = 1);'應該是'else if(Option == 1);'。第一個是轉讓,第二個是測試。 – Galik

+0

以及我已經修復,但我現在得到的錯誤是不同的 –

回答

4

你有一些基本的語法錯誤。我會列出其中的一些,但實際上,您的任務是: 1)閱讀第一個錯誤消息... 2)查找關於您正在使用的關鍵字和表達式的C++書籍(或谷歌)該行... 3)仔細看,看看有什麼是關於你如何做 4)用它來解決這個問題在你的代碼

例如不同: (Option =< 1 && Option >=3)

如果你查看「小於或等於」和「大於或等於」操作,你會發現它們始終是<=>=而不是=< 所以,你的表達應該是:

(Option <= 1 && Option >=3)

你看到一個字符的區別在那裏?

對於這一個:

if (Option =< 1 && Option >=3) 
{ 
    cout<<"Error!"; 
    cout<<"You've enter a wrong Option"; 
} 
{ 
    else if (Option = 1); 

如果你閱讀了關於if/else語句,你會看到,結構永遠是:

if (condition) 
{ 
    // stuff here 
} 
else if (condition2) 
{ 
    // more stuff here 
} 

看看你的花括號怎麼看你的碼。 他們看起來非常不同。 大括號應該始終圍繞的代碼塊,並應與{開始和結束} - 你don't.You有}緊跟着一個{然後有其他部分......這僅僅是全力以赴重擊。

嘗試複製你的代碼...並刪除所有做的東西的語句...和只是保持if/elses和大括號,看看他們排隊......排隊捲曲大括號應該很容易做,一旦額外的代碼不存在...然後你可以添加代碼語句

相關問題