2

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));
     }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lowriderzxxx
  • 163
  • 8
  • An alternative solution would be to combine the two `cout`s: `cout << answer << " is the " << n << "nd Fibonacci number\n";` and repeat this for all occurrences. – Markus Meskanen Jul 11 '13 at 08:09
  • Because, well, you have an else, without an if. – PlasmaHH Jul 11 '13 at 08:16
  • 1
    Because you are not using braces properly (in `main`; but `fib` is OK). Copy-paste your code in a text editor and run the "Auto Indent" or "Format code" command, and the indentation will show you that if not using braces, only the first statement after an `if` is bound to the conditional. (See answers below for fixed code...) – gx_ Jul 11 '13 at 08:21
  • 1
    For next time, keep in mind to also post the line numbers where you are getting the compilation error and indicate that line in the source code. – phant0m Jul 11 '13 at 09:13
  • Related (not duplicate - but would hint at a way to not rely on inspection): *[Why is GCC warning me this line is "misleadingly indented as if it were guarded by" an if?](https://stackoverflow.com/questions/50318900/)* – Peter Mortensen May 22 '22 at 12:48
  • Even in 2013 this was a mega duplicate (as nearly every beginner in C or C++ makes this mistake). What is the canonical question? – Peter Mortensen May 22 '22 at 12:50
  • Other low-scored duplicates (perhaps they can be leads for finding the canonical question): 1) *[misleading indentation; statement is not part of the previous 'if' \[-Werror,-Wmisleading-indentation\]](https://stackoverflow.com/questions/68443529/)*, 2) *[Compilation error in C just because of indentation?](https://stackoverflow.com/questions/66198475/)*, 3) *[I don’t understand the results that are getting printed](https://stackoverflow.com/questions/50695635/)*, 4) *[Why must this 'if' block with a single statement ...bracket to work properly?](https://stackoverflow.com/questions/60892538/)* – Peter Mortensen May 22 '22 at 13:44
  • cont' - 5) *[Issue with C program (maybe solved with use of arrays)](https://stackoverflow.com/questions/43184380/)*, *[Creating a program in C to generate primes](https://stackoverflow.com/questions/40202703/)*. – Peter Mortensen May 22 '22 at 14:39
  • For Java (perhaps there is a generic canonical): 1) *[Confusing indentation, why and how it can happen](https://stackoverflow.com/questions/34088468/)* and 2) *[Scope of a 'for' loop](https://stackoverflow.com/questions/11999624/)* – Peter Mortensen May 22 '22 at 14:39

8 Answers8

4

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;
}
Edorka
  • 1,781
  • 12
  • 24
3

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruce
  • 7,094
  • 1
  • 25
  • 42
2

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";
     }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gx_
  • 4,690
  • 24
  • 31
  • @MarkusMeskanen As I said, I just copy-pasted the code into an IDE code editor and used the "Auto Indent" command... I answered the question "Why do i get a compiling error", I didn't bother to correct the use of braces – gx_ Jul 11 '13 at 07:58
  • @A.S.Roma _Please read the whole answer and comments._ I did not _personnaly_ format the code, I used an _automated command_. Thank you. (Not for the downvote omg -_-) – gx_ Jul 11 '13 at 08:00
  • @gx_ I know you didn't format the code, but it's wrong (doesn't compile, no problem for downvote). So, why bother answering? Besides you're mentioning it's *properly indented* when it's not. – Markus Meskanen Jul 11 '13 at 08:05
  • @MarkusMeskanen Ok, I reworded. Better now? :) (As for "doesn't compile", the question was "why do I get an error", not "fix my code" ;)) – gx_ Jul 11 '13 at 08:10
  • @gx_ It's better, but it's still not good. The only thing you're doing is telling him what your IDE does to his code with auto-indent, the code still won't compile. Not going to remove my downvote. – Markus Meskanen Jul 11 '13 at 08:14
  • @MarkusMeskanen (btw I was not begging you to un-downvote) Ok I'm just going to replace this "answer" with a comment (the "plus" of the answer was to embed code directly but whatever). – gx_ Jul 11 '13 at 08:17
  • @ commenters Well I couldn't help but keeping unsatisfied (even though I upvoted some good answers and comments here), so I eventually came back and rewrote my answer entirely (I didn't want to post a new one, but I'm not sure what's the custom on SO...). I hope lowriderzxxx will understand better now – gx_ Jul 11 '13 at 09:23
2

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.

Brian
  • 4,958
  • 8
  • 40
  • 56
2

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));
     }
}
lulyon
  • 6,707
  • 7
  • 32
  • 49
1

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";
}
Alex1985
  • 658
  • 3
  • 7
1

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'......

Martin James
  • 24,453
  • 3
  • 36
  • 60
0

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

    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lowriderzxxx
  • 163
  • 8
  • But there is a lot of redundancy in the code, e.g., `cout << answer << " is the " << n;` and `Fibonacci number`\n are repeated ***four*** times. The last three could be replaced by ***fixed*** strings ("3rd", "2nd", and "1st"). `n < 3 && n > 1` could be simplified to `n == 2`. – Peter Mortensen May 14 '22 at 14:48