Factorial Help

Oct 25, 2011 at 3:38am
I'm having 2 problems. First of all I can't figure out why it's not displaying the factorial of number. When I try to compile this code I get an error on the line cout << "Factorial of " << number << " is " << factorial(number) << endl; but I can't figure out whats wrong for the life of me.

Also its repeating "Enter a number between 2 and 30: " regardless of what number I enter. The only time I want it to repeat this is when the number is not between 2 and 30.

Any help is much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>


using namespace std;
int factorial (int);

int main()

{
    int number;
    do {
    cout << "Enter a number between 2 and 30: ";
    cin >> number;
    cout << "Factorial of " << number << " is " << factorial(number) << endl;;
    
    
}
   while (number );
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Oct 25, 2011 at 3:46am
Where is the implementation of the factorial function??
Oct 25, 2011 at 3:51am
Im sorry I'm not sure what you mean. I'm very new to this.
Oct 25, 2011 at 5:06am
@Nexrus

You declared a function named
int factorial (int);
but the routine to get the factorial, is not there. You should have a
1
2
3
4
5
6
7
8
9
10
11
12
13
// declare another variable
int fact;
// After your cin >> number, put in
fact = factorial(number);
cout << "Factorial of " << number << " is " << fact << endl;


int factorial( number)  // The called function
{
int factorial; 
// code to get the factorial
return factorial; // This value is returned as the variable fact 
}
Oct 25, 2011 at 1:19pm
This is my new code. I don't get any errors when I compile it but the exe will not run either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int factorial (int);


int main()


{
    int number;
    int fact;
    
    do {
    cout << "Enter a number between 2 and 30: ";
    cin >> number; 
    fact = factorial(number);
    cout << "Factorial of " << number << " is " << fact << endl;
    
}
   while (number);
    system("PAUSE");
    return EXIT_SUCCESS;
}
Oct 25, 2011 at 1:57pm
@Nexrus

You still haven't written the routine to show the factorial. As I said, you declared a function named
int factorial( int );
but you never created it. You still need the
1
2
3
4
5
6
int factorial( number)  // The called function
{
int factorial; 
// code to get the factorial
return factorial; // This value is returned as the variable fact 
}
.
Oct 25, 2011 at 1:58pm
You declared a function "factorial", but it has no meaning, as you haven't defined it.
Imagine I want a function to double my number:
1
2
3
4
5
6
7
8
9
10
11
int doubleMyNumber(int); // Declares function

int main() {
  int myNumber = 8
  int doubleNumber doubleMyNumber(myNumber); // Calls function
  return 0;
}

int doubleMyNumber(int t) { // Defines function
  return t*2;
}

Each 'step' has a point:
-Declaration tells the compiler the function exists.
-Calling tells the compiler the function has to be executed (and with which parameters).
-Defining tells the compiler what the function does, in this case: returning the double of a number.

Declaration and definition can be done simultaneously (i.e. move the definition upwards before the first time it's called), but splitting both can improve readability.
Last edited on Oct 25, 2011 at 1:59pm
Oct 25, 2011 at 5:27pm
I get the impression that Nexrus believes that C++ has a math function called 'factorial' that can be implemented at will in the same way there are math functions called 'power', 'sine', or 'cosine'.

@Nexrus,

There is NO math function called 'factorial' in C++. You must define it yourself by writing code for it as Whitenite1 and Gaminic explained in their posts.
Last edited on Oct 25, 2011 at 5:28pm
Oct 25, 2011 at 6:13pm
Thats exactly what I thought. Let me take the info you guys have shared with me and see what I can do. Thank you very much.
Oct 26, 2011 at 12:31am
I'm not entirely sure how but I messed around with it until I finally got it to give me the factorial of the number I enter. Now I need to use do-while loop so that if I enter any number thats not between 2-15 it will cout "Enter a number between 2 and 30: "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int number;

int factorial (int a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return (1);
}

int main()
{
    cout << "Enter a number between 2 and 30: ";
    cin >> number; 
    cout << "Factorial of " << number << " is " << factorial (number) << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
Oct 26, 2011 at 3:37am
@Nexrus, congrats on the program. I don't know anything about factorials, so I don't really know if the results are correct, but I'll take your word, ( and program ) they are.
Topic archived. No new replies allowed.