#include <iostream>
usingnamespace std;
int factorial(int);
int n;
string negative = "Input is too large.";
string toolarge = "No negative numbers.";
int main(){
cout << "Enter your factorial" << endl;
cin >> n;
cout << factorial(n) << endl;
}
int factorial(int n){
if ( n == 1 ){
return 1;
}
elseif ( n > 1 && n < 26 ){
return n * factorial(n-1);
}
elseif ( n > 25 ){
return toolarge;
}
else ( n < 1 ){
return negative;
}
}
The meat of the functions are actually fine, for example if I input 5, it returns the correct 120, but in the conditional statements in accepting the value of int n, if I put in 50, it returns the string toolarge and negative, in separate lines.
How is this supposed to compile? toolarge and negative are strings while your function returns int. There is no implicit conversion.
There are several ways to do this, but the simplest one would be to handle n > 25 and n < 1 cases before calling factorial().