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.
#include <iostream>
usingnamespace 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;
}
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
}
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace 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;
}
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
}
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.
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.
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: "
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int number;
int factorial (int a)
{
if (a > 1)
return (a * factorial (a-1));
elsereturn (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;
}
@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.