2013-07-11 54 views
2

當我嘗試編譯代碼我得到那個說else without a previous if錯誤:爲什麼我得到一個編譯錯誤,說錯誤:'else'沒有以前的'if'?

// Fibonacci series using recursion 
#include <iostream> 
using namespace std; 
int fib (int n); 

int main() 
{ 
    int n, answer; 

    cout << "\n\n\t\tEnter number to find: "; 
    cin >> n; 
    cout << "\n\n"; 

    answer = fib(n); 

    if(n < 3 && n > 1) 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 
    { 
     if(n < 3) 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
     else 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 
    } 
    else 
    cout << answer << " is the " << n; 
    cout << "th Fibonacci number\n"; 
    return 0; 
} 

int fib (int n) 
{ 
    cout << "Processing fib (" << n << ")... "; 

    if (n < 3) 
    { 
     cout << "Return 1!\n"; 
     return 1; 
    } 
    else 
    { 
     cout << "Call fib(" << n-2 << ") "; 
     cout << "and fib(" << n-1 << ").\n"; 
     return(fib(n-2) + fib(n-1)); 
    } 
} 
+0

另一種解決辦法是將兩個'COUT結合's:'cout << answer <<「是」<< n <<「nd斐波那契數\ n」;'並且對所有出現的重複這個。 –

+0

因爲,好吧,你有別的,沒有if。 – PlasmaHH

+0

因爲你沒有正確地使用大括號(在'main';但'fib'確定)。在文本編輯器中複製粘貼代碼並運行「自動縮進」或「格式代碼」命令,並且縮進將顯示如果不使用大括號,則只有條件後面的「if」後的第一個語句被綁定。 (有關固定代碼,請參閱下面的答案...) –

回答

2

編輯:。這是一個完全重新設計的答案看評論謝謝你,對不起了「爭議」。 )

因爲你沒有正確地在你的main使用大括號( 「大括號」,{})。

首先讓我們的代碼的這個內部:

 if(n < 3) 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
     else 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 

即當前縮進是「錯誤」和誤導。如果您將其複製粘貼到一個代碼編輯器,並使用自動格式化(自動 - 縮進就足夠了),你會得到:

 if(n < 3) 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
     else 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 

這表明你真正的代碼的「意義」。加括號和空白線清晰後:

 if(n < 3) 
     { 
     cout << answer << " is the " << n; 
     } 

     cout << "st Fibonacci number\n"; 

     else 
     { 
     cout << answer << " is the " << n; 
     } 

     cout << "rd Fibonacci number\n"; 

正如你所看到的,只有第一個cout語句由if條件。第二個將始終執行。然後出現一個else,它遵循「普通」,「無條件」語句,而不是「條件化」語句/語句塊(作爲整體,語句塊也是語句)。

爲了解決這個問題的一部分,必須用所有的條件語句中括號:

 if(n < 3) 
     { 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
     } 
     else 
     { 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 
     } 

或在更緊湊的風格:

 if(n < 3) { 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
     } else { 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 
     } 

使得全塊語句是條件。

現在, 「內部」 的if-else部分是固定的,讓我們的 「外部」 的if-else:

 if(n < 3 && n > 1) 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 
    { 
     /* ... fixed inner if-else ... */ 
    } 
    else 
    cout << answer << " is the " << n; 
    cout << "th Fibonacci number\n"; 

讓我們再次使用一個代碼格式化:

 if(n < 3 && n > 1) 
     cout << answer << " is the " << n; 
    cout << "nd Fibonacci number\n"; 
    { 
     /* ... fixed inner if-else ... */ 
    } 
    else 
     cout << answer << " is the " << n; 
    cout << "th Fibonacci number\n"; 

實含義現在應該清楚(使用簡潔的風格在這裏):一個人在中間

 if(n < 3 && n > 1) { 
     cout << answer << " is the " << n; 
    } 

    cout << "nd Fibonacci number\n"; 

    { 
     /* ... fixed inner if-else ... */ 
    } 

    else { 
     cout << answer << " is the " << n; 
    } 

    cout << "th Fibonacci number\n"; 

有趣塊(內碼的文胸ces但不直接跟在if/else之上)實際上是一個匿名塊,它只引入一個內部作用域(在關閉}之後,內部定義的變量不再存在)。它可以看作是一個簡單的陳述(無條件的),就像它上面的cout << "nd Fibonacci number\n";一樣。

再次,解決方法是顯而易見的:

 if(n < 3 && n > 1) { 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 

     /* ... fixed inner if-else ... */ 

    } else { 
     cout << answer << " is the " << n; 
     cout << "th Fibonacci number\n"; 
    } 
+0

這甚至不是正確的縮進,請檢查Edorka的答案。 –

+0

@MarkusMeskanen正如我所說的,我只是將代碼複製粘貼到IDE代碼編輯器中,並使用「自動縮進」命令...我回答了「爲什麼我會收到編譯錯誤」這個問題,我沒有打擾正確使用大括號 –

+1

這是錯誤的。如果你是第二個人,如果錯誤 – Brian

4

當然這是一個關於關鍵括號問題:後

int main() 
{ 
    int n, answer; 

    cout << "\n\n\t\tEnter number to find: "; 
    cin >> n; 
    cout << "\n\n"; 

    answer = fib(n); 

    if(n < 3 && n > 1) { 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 

     if(n < 3) { 
      cout << answer << " is the " << n; 
      cout << "st Fibonacci number\n"; 
     } else { 
      cout << answer << " is the " << n; 
      cout << "rd Fibonacci number\n"; 
     } 
    } 
    else { 
     cout << answer << " is the " << n; 
     cout << "th Fibonacci number\n"; 
    } 
    return 0; 
} 
+3

通過代碼格式化程序來運行它會讓錯誤變得明顯。例如,在vim中:'gg = G' – tjameson

3

你丟失的榮譽(大括號)您如果

if(n < 3 && n > 1) 
    cout << answer << " is the " << n; 
    cout << "nd Fibonacci number\n"; 
{ 
    if(n < 3) 

表示

if(n < 3 && n > 1) 
{ 
    cout << answer << " is the " << n; 
} // end of if 
cout << "nd Fibonacci number\n"; // always executed 
{ // new anonymous block 
    if(n < 3) 
2

試試這個:

int main() 
{ 
    int n, answer; 

    cout << "\n\n\t\tEnter number to find: "; 
    cin >> n; 
    cout << "\n\n"; 

    answer = fib(n); 

    if(n < 3 && n > 1) 
    { 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 

     if(n < 3) 
     { 
      cout << answer << " is the " << n; 
      cout << "st Fibonacci number\n"; 
     } 
     else 
     { 
      cout << answer << " is the " << n; 
      cout << "rd Fibonacci number\n"; 
     } 
    } 
    else 
    { 
     cout << answer << " is the " << n; 
     cout << "th Fibonacci number\n"; 
    } 
    return 0; 
} 

確保您if S和else ES是相應的大括號內。

1

您忘記在內部if上使用{}。它應該是

if(n < 3) 
{ 
    cout << answer << " is the " << n; 
    cout << "st Fibonacci number\n"; 
} 
else 
{ 
    cout << answer << " is the " << n; 
    cout << "rd Fibonacci number\n"; 
} 
1

我相信你失蹤後,大括號,如果(N < 3),所以條件只適用於線下。然後編譯器命中'else'......

2

你沒有在多語句的某些if else子句中添加括號。不要在真實世界的編碼中做到這一點。如下 更改代碼:

// Fibonacci series using recursion 
#include <iostream> 
using namespace std; 
int fib (int n); 

int main() 
{ 
    int n, answer; 

    cout << "\n\n\t\tEnter number to find: "; 
    cin >> n; 
    cout << "\n\n"; 

    answer = fib(n); 

    if(n < 3 && n > 1) 
     { 
     cout << answer << " is the " << n; 
     cout << "nd Fibonacci number\n"; 

     if(n < 3) 
    { 
     cout << answer << " is the " << n; 
     cout << "st Fibonacci number\n"; 
    } 
     else 
     { 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 
     } 
    } 
    else { 
    cout << answer << " is the " << n; 
    cout << "th Fibonacci number\n"; 
    } 
    return 0; 
} 

int fib (int n) 
{ 
    cout << "Processing fib (" << n << ")... "; 

    if (n < 3) 
    { 
     cout << "Return 1!\n"; 
     return 1; 
    } 
    else 
    { 
     cout << "Call fib(" << n-2 << ") "; 
     cout << "and fib(" << n-1 << ").\n"; 
     return(fib(n-2) + fib(n-1)); 
    } 
} 
0

所有你對大括號的答案是正確的,但我需要的代碼是這樣的人:

if (n > 3) 
{ 
    cout << answer << " is the " << n; 
    cout << "th Fibonacci number\n"; 
} 
else 
{ 
    if(n == 3) 
    { 
     cout << answer << " is the " << n; 
     cout << "rd Fibonacci number\n"; 
    } 
    else 
    { 
     if(n < 3 && n > 1) 
     { 
      cout << answer << " is the " << n; 
      cout << "nd Fibonacci number\n"; 
     } 
     else 
     { 
      cout << answer << " is the " << n; 
      cout << "st Fibonacci number\n"; 
     } 

    } 
}