Factorial, whats wrong?

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
27
28
29
30
31
#include <iostream>

using namespace 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;
    }
    else if ( n > 1 && n < 26 ){
        return n * factorial(n-1);
    }
    else if ( 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().
Thank you, I subtracted the strings, put those conditionals in the main function after the input, and just made it a simple cout.
Topic archived. No new replies allowed.