I am new to this website; in fact this is my first question. So if I am asking this question in the wrong context, please inform me so I can make the adjustments. I have a program that is created in Visual Studio 2010 to allow a user to enter in an integer from 1 to 10. That user will click a button which in turn should compute and display the integer's factorial value in a text box. I thought everything looks correct but nothing is appearing in the text box.
The following code is what I have been using:
#pragma endregion
private: System::Void btnFactorial_Click(System::Object^ sender, System::EventArgs^ e)
{
int n;
int nfact = 1;
Int32::TryParse(txtNum->Text, n);
if (n > 0)
{
for (int i = 1; i <= n; i++)
nfact *= i;
txtResult->Text = nfact.ToString();
}
else
MessageBox::Show ("Please enter a value > 0");
}
private: int Factorial (int n)
{
if (n == 0)
return 1;
else
return n * Factorial (n-1);
}
};
}
Can I ask for some help as to what I am doing wrong here?
You need to call your own function somewhere. Replace this:
for (int i = 1; i <= n; i++)
nfact *= i;
and write this:
nfact = Factorial(n);