Recursive Function to determine the #of even digits in an integer

I am new to C++ and i wrote a recursive function to display the number of even digits in an integer. I dont understand why it is not working. Any help would be much appreciated. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int numEven (int x)
        if (x % 2 == 0) return numEven(x/10) + 1;
        return numEven(x/10); }

int main () {
        int x;
        cout << "Input a number to find the number of even digits \n";
        cin >> x;
        if (x<=0) return 0;
        numEven(x);
        return 0;
}                  
http://www.cplusplus.com/forum/articles/40071/#msg218019


you don't have a base case.
you do nothing with the returned value
What about now with this code.
#include <iostream>
using namespace std;

int numEven (int x) {
int count=0;
if (x<=0) return 0;
if (x % 2 == 0){
count++;
return numEven(x/10) + 1;}
return numEven(x/10);
cout << count;}

int main () {
int x;
cout << "Input a number to find the number of even digits \n";
cin >> x;
numEven(x);
return 0;
}
Topic archived. No new replies allowed.