Arggghh... Learning to use recursive functions :/

Hi guys, I'm trying to use a recursive function to drop the last digit and enter a space, it would be like this:

Enter a number: 123

Output:
3
2
1

I was trying to do it but it gives me errors... and by the way, why can't I assign a double to my numb variable and then write this:
get_whole_decimal = (numb / 10);

if I put it like that it gives me errors, it's like if it cannot accept doubles.


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
#include <iostream>
using namespace std;

//Prototypes
double get_last_numb(double);



int main()
{
	double user = 0.0, get_numb;

	cin >> user;

	get_numb = get_last_numb(user);

	cout << get_numb;

	return 0;
}

double get_last_numb(int numb)    //In case the imput were: 123
{
	double get_last_digit = 0.0, decimal_last_digit = 0.0, get_whole_decimal=0.0, drop_last=0.0;

	//Base case
	if (numb < 10)
		return numb;
	else

	get_whole_decimal = (numb / 10);							//I get 12.3
	get_last_digit=(numb % 10);									//I got 3
	cout << get_last_digit << endl;								//Displays 3
	decimal_last_digit=(get_last_digit / 10);					//I get 0.3
	drop_last = (get_whole_decimal - decimal_last_digit);		//I get 12
	drop_last = numb;											//12=the new numb
	

	return (get_last_numb(numb));

	


}
Last edited on
Hello,

It would be helpful if you could tell us what errors you're getting, as that helps us narrow down what part of your code is broken. That being said, you did well to include your source code. I can see a few problems right now. First, you prototype get_last_numb() as taking a double, but then you define it as taking an int. This is likely the source of some of your errors. Second, If I'm reading what you're trying to do correctly, line 36 needs to be numb = drop_last;. Remember that the object being stored TO is always the object on the left. As it is, you're storing the result from line 35 into drop_last, then immediately overwriting it in line 36. Third, because of the second problem, numb never gets changed. This will cause your recursion to never hit the base case, and if the program were compiled and run, it would crash with a stack overflow or segmentation fault; probably the former.

Hope that helps,
LupusNoctu
If you are looking to do this with POSITIVE INTEGERS you can do something as simple as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program for POSITIVE INTEGERS
#include <iostream>

void printReverseRecursive(int number);

int main()
{
    int input;
    std::cout << "Enter a number: ";
    std::cin >> input;
    
    printReverseRecursive(input);
    
    return 0;
}

void printReverseRecursive(int number)
{
    if(number) //not equal to 0
    {
        std::cout << number % 10 << std::endl;
        printReverseRecursive(number / 10);
    }
}
Enter a number: 123
3
2
1


Alternatively you could do this with a basic loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program for POSITIVE INTEGERS
#include <iostream>

void printReverseLoop(int number);

int main()
{
    int input;
    std::cout << "Enter a number: ";
    std::cin >> input;
    
    printReverseLoop(input);
    
    return 0;
}

void printReverseLoop(int number)
{
    while(number) //not equal to 0
    {
        std::cout << number % 10 << std::endl;
        number /= 10;
    }
}
Enter a number: 123
3
2
1



Too help with recursive you are going to want to maybe map it out with pen and paper until you are comfortable.
LupusNoctu thank you for nailing it down, good explanation!

giblit I will write it in paper and try it like that, thanks for your time! Now I'll try to do it backwards like
1
2
3

Thanks guys!
Last edited on
Topic archived. No new replies allowed.