Hello, I'm trying to create a program to calculate the factorial of variable int input. I am using xcode, and it's giving me an error saying "factorial was not declared in this scope" on the line within the else statement. I have found other programs on the internet, but I would simply like to understand why mine won't work. I would be grateful for any constructive suggestions or comments.
Thanks.
Here is what I have so far:

#include <iostream>

using namespace std;

int main(int input)
{
    cin >> input;

    if (input == 0) 
    {
        cout << "1" << endl;
    }

    else
    {
        cout << factorial << endl;
    }

}

int factorial(int input)
{
    cin >> input;
    input * (input - 1);
}

Dani AI

Generated

Quick diagnosis (what caused the compiler error and the wrong result)
The thread shows three separate issues: a wrong main signature, reading input inside the factorial routine, and functions that don't return a value on every path. was right about the declaration/prototype issue, 's recursion shows the correct mathematical structure, and 's loop approach avoids recursion's stack use. The concrete fixes are: use a proper int main() (or int main(int argc, char**)), read input in main, make factorial a pure function (take an argument and return the computed value), and always return a value (including the base case).

A compact, safe iterative pattern
Put input handling in main, use a wide integer type for small factorials, and validate input. Below is a simple, clear iterative example that differs from earlier snippets but illustrates these points.

#include <iostream>

using namespace std;

unsigned long long factorial(unsigned int n) {
    unsigned long long acc = 1ULL;
    for (unsigned int i = 2; i <= n; ++i) acc *= i;
    return acc;
}

int main() {
    unsigned int n;
    if (!(cin >> n)) return 0;          // invalid input
    if (n > 20) cerr << "Warning: 64-bit overflow possible for n > 20\n";
    cout << factorial(n) << '\n';
    return 0;
}

Additional notes and troubleshooting
Do not call cin from inside factorial. If recursion is preferred, ensure a base case returns 1 and every path returns a value; remember deep recursion can hit the call stack for large n. For values larger than ~20, use an arbitrary-precision library (e.g., Boost.Multiprecision) or a big-integer routine; otherwise the 64-bit result will overflow. Enabling compiler warnings (e.g., -Wall -Wextra) will catch many of the original mistakes early.

Recommended Answers

All 8 Replies

There are multiple problems:
You need a forward declaration of factorial since it is called before it is declared (add the line 'int factorial(int input);' after the 'using...' line)
You have to write factorial as a function call with input as the argument (as opposed to a variable):

cout << factorial(input) << endl;

Also, factorial does not have a return statement to return the number so it won't print, you would need to add 'return input;' at the end of the function.

Also, please use code tags when posting code.

Well, and the body of function factorial(...) should be better completely replaced by

{
  if(input<=1) return 1;
    else return input*factorial(input-1);
}

Ok I took a bit of all of your advice and revised it so there is no longer an error, but now I realize it's it's not looping the multiplication, for example, "5" (enter) = "20," not 120, which is 5!
Thanks!
Here is the revised code:

#include <iostream>

using namespace std;

int factorial(int input)
{
	cin >> input;
	
	if(input == 0)
	{
		cout << "1" << endl;
	}
	
	else
	{
		return input * (input - 1);
	}
}

int main(int input)
{
	cout << factorial(input) << endl;
}

That is because the factorial function is only multiplying the input by itself minus 1. You would need to do something similar to what tesuji suggested. Also the input doesnt need to be a commanline argument, just leave it as an argument to factorial and put the 'cin' command in main (like you had it structured before)
Alternatively, for a quick fix, you can just change line 16 with:

for(int i=input-1;i>1;i--) {
    input *= i;
}

well jwebb, this is kind of "disimproving".

Why didn't you exactly do what I suggested?

int factorial(int input)
{
// my suggestion and NOTHING else!
}
int main(){
int input;
cin >> input;
cout << factorial(input);
return 0;
}

-- tesu

Recursion is cool but when I was testing out using loops vs it any number 4 and above took longer to calculate when using the recursive function (time taken is not noticeable) and recursion takes up more memory because it keeps adding to the stack.

This is the code I used

unsigned int factorial( unsigned int num )
{
	if( num == 0 || num == 1 )
		return 1;
	unsigned int result = 1;
	for( unsigned int i = num; i > 1; i-- )
		result *= i;
	return result;
}

I used unsigned int just because you cannot use negative values or get a negative value from the factorial function.

Ok so I changed the code according to tesuji's suggestion, and it works! But isn't that recursion? I was trying to get around that. But thank you!
This is my first real experience posting to a forum, and I'm really pleased with the speed of responses and quality of assistance thus far, so thank you!
jwebb

Here's the code:

#include <iostream>

using namespace std;

int factorial(int input)
{
	
	if(input == 0)
	{
		return 1;
	}
	
	else
	{
		return input * factorial(input - 1);
	}
}

int main(int input)
{
	cin >> input;
	cout << factorial(input) << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.