Recursive function with two different data types

Hi,
I am having problems with generating a simple recursive function bool intTest(int n, char c) that will determine if a digit is (return 1) or isn't (return 0) part of a number.
The code I came up with works fine if the data type of the digit c is int, but not if type char is used; in every case, the functions returns 1.
Are there any obvious conversion techniques I am missing?(for '1' to 1 I used c-'0')
Also, is there a problem with cin>>c ?!

Thanks!!

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



bool intTest(int n, char c) {
	
	int ci = c - '0';
	if (n%10 == ci)
		return(1);
	else
		if ((n/10) > 0)
			intTest((n/10),c);
		else
			return(0);
	}

int main(){
	while(true){
	int n; char c;

	cout << "Specify Number ...";
	cin >> n;

	cout << "Specify Digit ...";
	cin >> c;
	
	intTest(n,c);

	if(intTest(n,c) == 0)
		cout << "Digit not part of Number." << endl;
	else 
		cout << "Digit part of Number." << endl;

	}
	return 0;
	}
Last edited on
Your code gives a warning when I try to compile it.
[Warning] control reaches end of non-void function [-Wreturn-type]


Under some circumstances your function intTest() does not return a value. Hint - see line 13.
Solved, thank you!!!!
1
2
if ((n/10) > 0)
return (intTest((n/10),c));
Topic archived. No new replies allowed.