Frage

When I try to compile the code I get an error that says else without a previous 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));
     }
}
War es hilfreich?

Lösung 2

You're missing accolades (curly brackets) after your if:

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

means

 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)

Andere Tipps

Surely it's a problem about the key brackets:

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;
}

Try this:

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;
}

Make sure that your ifs and elsees are accordingly within the curly brackets.

You did not add brackets in some if else clause with multiple statement. Don't do this in real world coding. Change the code as follows:

// 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));
     }
}

Because you didn't properly use braces ("curly brackets", { and }) in your main.

First let's take this inner part of the code:

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

That current indentation is "wrong" and misleading. If you copy-paste it into a code editor and use auto-formatting (auto-indent will suffice), you'll get:

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

which shows you the real "meaning" of the code. After adding braces and blank lines for clarity:

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

      cout << "st Fibonacci number\n";

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

      cout << "rd Fibonacci number\n";

As you can see, only the first cout statement is conditioned by the if. The second one will always be executed. Then comes an else that follows a "plain", "unconditional" statement, not a "conditioned" statement/block (a block of statement(s) as a whole is a statement too).

To fix this part you must wrap all the conditioned statements in braces:

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

or in a more compact style:

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

such that the full block-statement is conditioned.

Now that the "inner" if-else part is fixed, let's take the "outer" 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";

Let's use a code formatter again:

     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";

The real meaning should now be clear (using compact style here):

     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";

The funny block alone in the middle (code inside braces but not directly following an if/else) is actually an anonymous block, which just introduces an inner scope (variables defined inside will not exist anymore after the closing }). It can be see as a plain statement (unconditional), just like the cout << "nd Fibonacci number\n"; just above it.

Once again, the fix is obvious:

     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";
     }

You forgot to use {} on internal if's. It should be

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

I believe you are missing curly brackets after the if(n < 3), so the conditional only applies to the line below. Then the compiler hits the 'else'......

All your answers about the curly brackets were correct, but the code I needed was like this:

if (n > 3)
{
    cout << answer << " is the " << n;
    cout << "th Fibonacci number\n"; // E.g., "5th Fibonacci number"
}
else
{
    if(n == 3)
    {
        cout << answer << " is the " << n;
        cout << "rd Fibonacci number\n";  // "3rd Fibonacci number"
    }
    else
    {
       if(n < 3 && n > 1)
       {
           cout << answer << " is the " << n;
           cout << "nd Fibonacci number\n"; // "2nd Fibonacci number"
       }
       else
       {
           cout << answer << " is the " << n;
           cout << "st Fibonacci number\n"; // "1st Fibonacci number"
       }

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top