Recursion- counting numbers- homework

hi guys!
its supposed to do this:
4. Recursion. Write a recursive function called digits that will return the number of digits in its parameter of type long. Your function should work for arguments that are negative or zero as well as positive.

but only returns 0!
i have no clue why its doing 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
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;

long digits (long);

int main(){
	long number, count;
	 
	 cout << "Enter an integer number (negative or zero is ok): ";
	 cin >> number;
	
	count = digits (number);
	 
	cout << "The number of digits in number "<<number<<" is " << count;
	
	return 0;
}

long digits (long number){
	
	int count; 
	
	if (number > 0){
		count+=1;
		digits(number/10);
	}
	
	else {
		return count;
	}
}
}
You need to think about the logic.
What will happen when number is -10 or 0 ?
What value will be returned ?
Maybe do this first with pen and paper.
the problem is each time digits gets called it sets count to a garbage value in most cases it seems to be 0,but you should always initialise variables,the logic is correct apart from entering 0 or a minus number then the if statement will never execute and the function will just return so changed it to enter a number greater than 0

what you want to do is put count outside the digits function so it won't be re-initialised each time the function is called

this should work

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 digits (int);

int main(){
	int number, coun; // changed count to coun probably better to pick a more suitable name but just named it that for demonstration 

	 cout << "Enter an integer number (negative or zero is ok): ";
	 cin >> number;

	coun = digits(number);

	cout << "The number of digits in number "<<number<<" is " << coun;

	return 0;
}

int count = 0;

int digits(int number){

	if (number > 0){
        count++;
		digits(number/10);
	}
		return count;
}



but to be honest you have the right idea
Last edited on
Registered users can post here. Sign in or register to post.
<rt id="pwjBlLZ"><small id="pwjBlLZ"></small></rt>
<acronym id="pwjBlLZ"></acronym><rt id="pwjBlLZ"></rt>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
<acronym id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></acronym>
<option id="pwjBlLZ"></option>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym><acronym id="pwjBlLZ"></acronym>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
  • 6512882421 2018-04-19
  • 4659652420 2018-04-19
  • 2967832419 2018-04-19
  • 8339042418 2018-04-19
  • 8147112417 2018-04-19
  • 2774752416 2018-04-19
  • 4316132415 2018-04-19
  • 6265742414 2018-04-19
  • 1875142413 2018-04-19
  • 4146552412 2018-04-19
  • 8205662411 2018-04-19
  • 959982410 2018-04-19
  • 7153742409 2018-04-19
  • 9349932408 2018-04-18
  • 6024052407 2018-04-18
  • 2113432406 2018-04-18
  • 7629172405 2018-04-18
  • 163882404 2018-04-18
  • 3515922403 2018-04-18
  • 5047802402 2018-04-18