Hello,

So my program (to calculate n factorial) works well, except for one thing, it won’t output correctly after 12. My other 2 projects working with factorials ( calculating the constant e and e^x) worked because I used data type long. I tried doing it with my program but it did not work, I am not sure I was doing something wrong.
This post has a solution which I ran and it has the same issue:
http://www.daniweb.com/forums/showthread.php?t=344&highlight=factorial

Can someone please help? My code is below:

#include "Factorial.h"		// include definition of class Factorial

// declaration and input phase

// validate the input
void Factorial::validate(int enteredInteger)
{

}
// constructor
Factorial::Factorial(int enteredInteger)
{
	setVariables(enteredInteger);
}
// set functions to initialize the variables
void Factorial::setVariables(int enteredInteger)
{
	nFactorial = 1;
	value = enteredInteger;
	factor = enteredInteger;
}
void Factorial::setN(int enteredInteger)
{
	n = enteredInteger;
}
int Factorial::getFactorial()
{
	return nFactorial;
}
int Factorial::getInteger()
{
	return n;
}
void Factorial::compute()
{
	int integer;

	cout << "Enter a nonnegative integer to compute its factorial: ";
	cin >> integer;
	validate(integer);
	setN(integer);

	do
	{
		factor = n - value;
		nFactorial *= factor;
		value += 1;
	} while (value < n);


	displayMessage();
}
void Factorial::displayMessage()
{
	cout << "The n factorial of " << getInteger() << "!" << " is " << getFactorial() << "." << endl;
}

I don't see where you declare member nFactorial, but you've pretty much answered your own question. Data type int or even long will not hold a value beyond 12!. You need to either use type double (good to about 21! IIRC), or, if your system supports it, a long long (64bits)

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.