0
    #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++ 相当陌生。

4

2 回答 2

3

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

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

int main()
{
    // function body, factor visible
}
于 2012-04-08T12:45:41.410 回答
0

您需要从主函数中取出因子函数并将代码放入手镯中。

于 2012-04-08T12:47:24.193 回答