Can't get Recursion to reset

I am trying to write a recursion program that will count the # of digits in an integer - and for some reason when I try to repeat the process - it won't reset the counter to zero - if I don't use a static int it doesn't work at all? what am I doing 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;


int getData();
int countDigits(int);
int rinse();

void main()
{
	int numDigits=0, nbr;
	nbr=getData();
	numDigits=countDigits(nbr);
	cout << numDigits << " digits were entered." << endl;
	rinse();
	system("pause");
	
	return;
}

int getData()
{
	int nbr;
	cout << "Enter a number" << endl;
	cin >> nbr;

return nbr;
}

int countDigits(int NBR)
{
static int count=0;
 



     if(NBR!=0)
	 {
 
          count++;
 
          countDigits(NBR/10);
 
    }
 



    return count;
 

}

int rinse()
{
	char choice;
	cout << "\nWould you like to calculate another? (Y or N)" << endl << endl;
	cin >> choice;
	if(choice=='y' || choice=='Y') 
		{
			main();
		}
	else
		{
			cout << "Thanks and have a great day!" << endl << endl;
			system("pause");
			exit(0);
		}



}
1
2
3
4
/*constexpr*/ int countDigits( int x )
{
    return ( 1 + ( x / 10 ? countDigits( x / 10 ) : 0 ) );  
}
Last edited on
1
2
3
4
5
6
#include <cmath>

int countDigits( int x )
{
    return log10(x)+1;
}


EDIT (OT):
The reason it does not reset is simply because you have declared the count variable as static. To make this function truly recursive, you don't need a counter variable, you simply need to implement what @vlad from moscow suggested; which I will unravel to a few lines:

1
2
3
4
5
6
7
8
9
int countDigits(int NBR)
{
	if(NBR!=0)
	{
 		return 1 + countDigits(NBR/10);
 	}

    return 0;
}
Last edited on
ok... sorry - im confused... what happens to the 1? how is it passing the count of itterations of the call back... there is nothing to store the count or pass the count? C++ doesn't seem to do or know things "automatically"
Last edited on
Smac89
To make this function truly recursive, you don't need a counter variable, you simply need to implement what @vlad from moscow suggested; which I will unravel to a few lines:

1
2
3
4
5
6
7
8
9
 int countDigits(int NBR)
{
	if(NBR!=0)
	{
 		return 1 + countDigits(NBR/10);
 	}

    return 0;
}



Your unravel code is wrong because for number 0 that is a valid number your unravel function will say that the number has 0 digits.
Last edited on
when I plugged thee "unravel code" in - i did it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int countDigits(int NBR)

{

	if(NBR!=0)

	{

		return 1+ countDigits(NBR/10);
	}

	else
	{
		return 0;
	}

}




and it works. One of the numbers for my test is 90009 and it returns a 5.
@scthread


I pointed out clearly enough why this code is wrong.
@vlad

ok... so your saying if i put the #0 in as my number it will be wrong... I see that now... hmmm.... this fixed that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
int countDigits(int NBR)

{

	if(NBR!=0)

	{

		return 1+ countDigits(NBR/10);
	}

	else
	{
		return 1;
	}

}



Your code is strange to me - you use "?" & ":" in it - I assume your "?" is for my data - but the ":"? I have seen it in other code as well but we have not learned to do that - what does it mean?
Last edited on
@scthread

What is not clear? How many digits does number 0 have? And what will return your function? Is 0 a valid number?
@scthread
Your code is strange to me - you use "?" & ":" in it - I assume your "?" is for my data - but the ":"? I have seen it in other code as well but we have not learned to do that - what does it mean?


It is the so-called conditional operator. For example

x = ( y > 0 ) ? y : 0;

is equivalent to

if ( y > 0 ) x = y;
else x = 0;
Last edited on
oh... we haven't been taught to use that. that looks like what my instructor would call code candy... thank you for the explanation.
I would still like some sort of explanation to the 1+ part of this solution - when I step thru it appears that 1 has become the counter - but I don't see how, since its not declared. Just looking for some understanding - so that I can process it in my head as something other than "it just works... so use it"...
One thing you have to know about recursion is that it acts as a stack data structure.
Once you call the function again, the value left in the function (in this case 1 + )
is pushed unto the stack. Once the base case is reached, the last value returned is
in turn added to the first value at the top of the stack and that top value is popped from
the stack...Until one value is returned

__main__():
function(19999)

function(19999/10)
return 1 +
function(1999/10)
return 1 +
function(199/10)
return 1 +
function(19/10)
return 1 +
function(1/10)
//Base case reached
return 1
//function (19/10) + 1 == 2
//function(199/10) + function(19/10) + 1 == 3
//function(1999/10) + function(199/10) + function(19/10) + 1 == 4
//function(19999/10) + function(1999/10) + function(199/10) + function(19/10) + 1 == 5


If you program in python as well, this will be familiar to you if you have used the reduce function
Last edited on
@ Smac89

Thank you! That makes so much sense! I really appreciate you taking the time to explain it!
Topic archived. No new replies allowed.