错误C2143:语法错误:缺少';'在“如果”之前

时间:2012-04-08 12:44:23

标签: c++

    #include <iostream>
    using namespace std;

    int factor(int n);

    int main()
    { 
        int f,n;

    // Get user input

        cout << "Enter an integer: ";
        cin >> n;

    // Call factorial function

        f = factor(n);

    // Output results

        cout << n << "! = " << f << endl;

        int factor (int n)
            if(n <=1)
            {
             return 1;
            }
            else
            {
             int c = n * (n-1);
             return c;
            }
     };

我收到错误C2143:语法错误:缺少';'在“如果”之前 如果我遗漏了一些简单的东西,我很好奇。我是C ++的新手。

2 个答案:

答案 0 :(得分:3)

您正在尝试在函数factor中定义函数main。这在C ++中是不允许的。此外,factor的函数体需要大括号:

int factor(int n)
{
    // function body
}

int main()
{
    // function body, factor visible
}

答案 1 :(得分:0)

你需要从主函数中获取因子函数并将代码放在手镯中。