Ummm... Recursion, what am I missing

Here is the function:
1
2
3
int Factorials::recursion(int userNumber){
	return userNumber != 0 ? userNumber * recursion(userNumber - 1) : 1;
}


All i get as output is the initialized userNumber value.
I have stepped through the program and oddly enough it looks
like it starts at zero and then simply counts up to userNumber.

I have overloaded recursion with a Factorials::recursion(); declaration but I have tested to see if there was interference there and everything seems with cool with the overloaded functions.
Last edited on
I just tested it like this:
1
2
3
int recursion(int userNumber){
	return userNumber != 0 ? userNumber * recursion(userNumber - 1) : 1;
}

It works fine.

Your overload must be interfering.
closed account (z05DSL3A)
I have stepped through the program and oddly enough it looks
like it starts at zero and then simply counts up to userNumber.


The way that it is codded, if you are putting a break point on the return line, then I would expect it not to break until it hits the end of the recursion (userNumber = 0), then you would step back up the stack. If you are watching userNumber, it will appear as you have described.

Is the returned value correct?

Also you should guard against a negative userNumber
Last edited on
helios: I've renamed the function to get rid of overload and still have the same problem.

Grey Wolf: negative numbers guarded against in the input function, am I missing something or wouldn't it return values starting at userNumber and then count down vs what its doing by starting at zero and counting up to userNumber? It only returns the userNumber, not the recursion of the userNumber.
entire code for your view pleasure...
main.cpp calls the function
 
Factorials::recursion()

header
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

class Factorials{
private:
	static int userNumber;
	static int recursion(int userNumber);
	static int getData(int &userNumber);
	static void showData(int &userNumber);
public:
	static void iteration();
	static void recursion();
};

//Factorial Class definitions
int Factorials::userNumber = 0;

int Factorials::getData(int &userNumber){
	cout << "Please enter a number between 1 and 10.\n>>";
	cin >> userNumber;
	while (userNumber < 1 || userNumber > 10){
		cout << "\nERROR: Out of Range\n>>";
		cin >> userNumber;
	}
	cout << "\n" << userNumber << "! is equal to: ";
	return userNumber;
}

void Factorials::showData(int &userNumber){
	cout << userNumber << "\n";
}

void Factorials::iteration(){
	getData(userNumber);
	for (int i = userNumber; i > 1; i--){
		userNumber *= (i-1);
	}
	showData(userNumber);
}

void Factorials::recursion(){
	getData(userNumber);
	Factorials::recursion(userNumber);
	showData(userNumber);
}

int Factorials::recursion(int userNumber){
	return userNumber != 0 ? userNumber * recursion(userNumber - 1) : 1;
}
closed account (z05DSL3A)
Change line 43 to:
userNumber = Factorials::recursion(userNumber);
Awesome...
Topic archived. No new replies allowed.