2014-03-12 56 views
-5

有人可以請解釋我在這段代碼中的上述錯誤?我在CodeBlocks中獲得它,當我使用Visual Studio時,它表示期待一個聲明。 非常感謝。其他沒有以前如果錯誤在c + +

#include <iostream> 
    using namespace std; 
    int main() 
    { 
     char fruits; 
     float sales; 

     cout << " Enter the First letter of the Fruit's name : "; 
     cin >> fruits; 

     cout << " Enter the amount of sales : "; 
     cin >> sales; 


     if (fruits == 'A' || fruits == 'a') 
      cout << " You are selling Apples "; 
      cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 

     else if (fruits == 'P' || fruits == 'p') 
      cout << " You are selling Pears"; 
      cout << " Your commission is " << sales*(5/100) << " rupees. "; 

     else if (fruits == 'O' || fruits == 'o') 
      cout << " You are selling Oranges "; 
      cout << " Your commission is " << sales*(5.5/100) << " rupees. "; 

     else if (fruits == 'G' || fruits == 'g') 
      cout << " You are selling Grapes "; 
      cout << " Your commission is " << sales*(6/100) << " rupees. "; 

     else 
      cout << " Invalid Entry. Try again. "; 


     return 0; 
    } 
+5

如何製作包含多個語句的塊?如果你不知道,你需要回到你的書,並從頭開始閱讀。 –

+3

C++不是Python,你知道嗎? – 2014-03-12 16:10:20

+0

這就好像有人在這裏爲Apple工作,不是嗎?缺少大括號連接到水果..至少在這裏,它會導致編譯器錯誤,而不是[goto失敗](http://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail -apples-ssl-bug-explained-plus-an-unofficial-patch /)運行時錯誤。 – stefan

回答

5

您需要在您的if語句中使用括號,這樣

if (fruits == 'A' || fruits == 'a') { 
     cout << " You are selling Apples "; 
     cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 
} else if (fruits == 'P' || fruits == 'p') { 

} // and so on 

因爲否則你if條款將只適用於下列下一行,這將使你的邏輯看起來是這樣的:

if() 
    cout << " I am only getting executed if the above 'if' statement is true "; 
cout << " I will always be executed, no matter what "; 

而且,通過這樣做,你的else聲明將在你的代碼的中間鬆,導致在你正在編譯的錯誤

if() 
    cout << " I am only getting executed if the above 'if' statement is true "; 
cout << " I will always be executed, no matter what "; 
else // but, else what? There is no 'if' behind me 
+0

謝謝,它修復了錯誤:)) –

4

這是因爲你有一個多行塊沒有封閉花括號。 C++將此解釋

if (fruits == 'A' || fruits == 'a') 
    cout << " You are selling Apples "; 
    cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 

這樣的:

if (fruits == 'A' || fruits == 'a') 
    cout << " You are selling Apples "; // Protected by `if` 
// Not protected by `if` !!! 
cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 

C++編譯器將忽略縮進;這就是爲什麼當您在第二個cout之後放置else時,它會被視爲else而沒有前面的if

要解決此問題,請將所有受if s保護的塊括在花括號中。即使在不需要大括號的情況下,養成養成習慣也是一個好主意,因爲if可以保護單個陳述。

+0

謝謝你,它的固定:)) –

3

在C++中,代碼

if (fruits == 'A' || fruits == 'a') 
    cout << " You are selling Apples "; 
    cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 

被解釋爲

if (fruits == 'A' || fruits == 'a') cout << " You are selling Apples "; 
cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 

換句話說,只有第一個語句由if聲明保護。第二條聲明無條件執行。這解釋了您收到的錯誤 - else if與之前的if聲明不匹配。

爲了解決這個問題,加括號周圍的if報表的機構:

if (fruits == 'A' || fruits == 'a') { 
    cout << " You are selling Apples "; 
    cout << " Your commission is " << sales*(4.5/100) << " rupees. "; 
} 

更一般地,縮進在C++無關的,所以一定要小心,以確保您的縮進你預期的意圖相符。

希望這會有所幫助!

+0

謝謝你詳細的答案。它修復了錯誤。謝謝 :)) –