为什么我会得到一个编译错误,上面写着error:“else”而没有前面的“if”
Why do I get a compiling error that says error: ‘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 << "nnttEnter number to find: ";
cin >> n;
cout << "nn";
answer = fib(n);
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
{
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
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));
}
}
这肯定是关于键括号的问题:
int main()
{
int n, answer;
cout << "nnttEnter number to find: ";
cin >> n;
cout << "nn";
answer = fib(n);
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
if(n < 3) {
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
} else {
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
}
else {
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
}
return 0;
}
如果:,您的后将缺少荣誉(花括号)
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
{
if(n < 3)
指
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
} // end of if
cout << "nd Fibonacci numbern"; // always executed
{ // new anonymous block
if(n < 3)
试试这个:
int main()
{
int n, answer;
cout << "nnttEnter number to find: ";
cin >> n;
cout << "nn";
answer = fib(n);
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
}
else
{
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
}
return 0;
}
请确保您的if
和else
相应地位于花括号内。
您没有在带有多个语句的if-else子句中添加方括号。不要在现实世界中进行编码。更改代码如下:
// Fibonacci series using recursion
#include <iostream>
using namespace std;
int fib (int n);
int main()
{
int n, answer;
cout << "nnttEnter number to find: ";
cin >> n;
cout << "nn";
answer = fib(n);
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
}
else {
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
}
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));
}
}
因为在main
中没有正确使用大括号("花括号"、{
和}
)。
首先,我们来看一下代码的内部部分:
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
当前的缩进是";错误的";以及误导。如果您将其复制粘贴到代码编辑器中并使用自动格式化(自动缩进就足够了),您将得到:
if(n < 3)
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
else
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
它向你展示了真实的";意思是";代码的。在添加大括号和空行以提高清晰度后:
if(n < 3)
{
cout << answer << " is the " << n;
}
cout << "st Fibonacci numbern";
else
{
cout << answer << " is the " << n;
}
cout << "rd Fibonacci numbern";
正如您所看到的,只有第一个cout
语句受到if
的制约。第二个将始终执行。接着是CCD_ 8;"普通"无条件的";声明,而不是";条件";语句/块(一个语句块作为一个整体也是一个语句)。
要修复这一部分,必须用大括号包装所有条件语句:
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
或者更紧凑的风格:
if(n < 3) {
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
} else {
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
使得完整的块语句是有条件的。
现在;内部";如果else部分是固定的,让我们取";外部";如果其他:
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
{
/* ... fixed inner if-else ... */
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
让我们再次使用代码格式化程序:
if(n < 3 && n > 1)
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
{
/* ... fixed inner if-else ... */
}
else
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
真正的含义现在应该很清楚了(这里使用紧凑风格):
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
}
cout << "nd Fibonacci numbern";
{
/* ... fixed inner if-else ... */
}
else {
cout << answer << " is the " << n;
}
cout << "th Fibonacci numbern";
中间单独的有趣块(大括号内的代码,但不直接在if
/else
后面)实际上是一个匿名块,它只引入了一个内部范围(内部定义的变量在关闭}
后将不再存在)。它可以被看作是一个简单的语句(无条件的),就像它上面的cout << "nd Fibonacci numbern";
一样
再一次,修复是显而易见的:
if(n < 3 && n > 1) {
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern";
/* ... fixed inner if-else ... */
} else {
cout << answer << " is the " << n;
cout << "th Fibonacci numbern";
}
您忘记在内部if上使用{}。应该是
if(n < 3)
{
cout << answer << " is the " << n;
cout << "st Fibonacci numbern";
}
else
{
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern";
}
我相信if(n<3)后面缺少大括号,所以条件仅适用于下面的行。然后编译器点击"else"。。。。。。
关于花括号的所有答案都是正确的,但我需要的代码是这样的:
if (n > 3)
{
cout << answer << " is the " << n;
cout << "th Fibonacci numbern"; // E.g., "5th Fibonacci number"
}
else
{
if(n == 3)
{
cout << answer << " is the " << n;
cout << "rd Fibonacci numbern"; // "3rd Fibonacci number"
}
else
{
if(n < 3 && n > 1)
{
cout << answer << " is the " << n;
cout << "nd Fibonacci numbern"; // "2nd Fibonacci number"
}
else
{
cout << answer << " is the " << n;
cout << "st Fibonacci numbern"; // "1st Fibonacci number"
}
}
}
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 使用if-else将数字转换为单词
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- c++ std::enable_if .... else?
- 如果 else 语句不断循环
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 为什么我会得到一个编译错误,上面写着error:“else”而没有前面的“if”