pgr
pgr

Reputation: 125

Simple C++ script - Factorial - Errors

I am learning C++. Here is a code counting factorial(silnia). Liczba means number.

#include <iostream>

using namespace std;

int _main() 
{
    int silnia;
    int n;
    if (n == 0) return 1;
    else;
    return n * silnia(n-1);

    int liczba;
    cout << "Podaj liczbe: ";
    cin >> liczba;
    cout << liczba << "! = " << silnia(liczba) << endl;
    return 0;
}

However I still become error message:

main.cpp: In function 'int _main()':
main.cpp:9:20: error: 'silnia' cannot be used as a function
 return n*silnia(n-1);
                    ^
main.cpp:14:42: error: 'silnia' cannot be used as a function
 cout << liczba << "! = " << silnia(liczba) << endl

What can be the problem?

Upvotes: 0

Views: 467

Answers (2)

maciekm
maciekm

Reputation: 257

delete this part from your main:

 int silnia;
 int n;
 if (n == 0) return 1;
 else;
 return n * silnia(n-1); 

put this function outside main function

 int silnia(int a)
{
 if(a==0) return 1;
 else return silnia(a-1)*a;
}

Upvotes: 1

djechlin
djechlin

Reputation: 60778

silnia(liczba)

Looks like you're trying to "call" the integer silnia. Exactly what your error message says. I'm not sure what you're trying to do in that line.

Upvotes: 0

Related Questions