Write a function named containsDigit
that determines if a number contains a particular digit.
The header should look like:
bool containsDigit(int number, int digit);
If number contains digit, then the function should return true
. Otherwise, the function should return false
.
Input:
147 9
Output:
false
I don't know why I always get false when I write like this:
bool containsDigit(int number, int digit);
int main() {
double con;
int number, digit;
cout << "Input a number and a digit:
";
cin >> number >> digit;
con = containsDigit(number, digit);
cout << con;
return 0;
}
bool containsDigit(int number, int digit) {
int a(0), b;
b = number;
while (number > 0) {
a = a + 1;
number = number / 10;
}
cout << a;
while (a > 1) {
a = a - 1;
if (b / pow(10, a) == digit) {
cout << "true
";
break;
} else {
if (a == 1)
cout << "false
";
else
cout << "";
}
b = b % pow(10, a);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…