问题
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));
}
}
回答1:
Your 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)
回答2:
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;
}
回答3:
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 if
s and else
es are accordingly within the curly brackets.
回答4:
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));
}
}
回答5:
(Edit: This is a completely reworked answer. See comments. Thank you and sorry for the "controversy".)
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";
}
回答6:
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";
}
回答7:
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'......
回答8:
All your answers about the curly brackets were correct but the code i needed was like this guys :
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";
}
}
}
来源:https://stackoverflow.com/questions/17587875/why-do-i-get-a-compiling-error-that-says-error-else-without-a-previous-if