I have a homework assignment that I am a little stuck on. Here are the instructions: Write a function bool IsOddDigit( char ch ); that takes a character and returns true if the character is one of the odd digits { '1', '3', '5', '7', '9' }, otherwise false. Write a main() program that calls IsOddDigit() with various characters, and uses the return value from IsOddDigit() to decide to print one of two messages, that the character is an odd digit or that the character is not an odd digit.
Test with at least all the digits '0' through '9', the letters 'a', 'b' and 'c' (both lower and upper case),
and several other characters such as punctuation marks and other special characters. You may use
any method you want to generate characters to send to IsOddDigit().
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;
bool IsOddDigit(char);
int main()
{
char ch;
cout << "Enter a character to check if it's odd: ";
cin >> ch;
if( IsOddDigit(ch))
{
cout << "The character is an odd digit." << endl;
}
else
{
cout << "The character is not an odd digit." << endl;
}
return 0;
}
bool IsOddDigit(char eval)
{
if( eval >= '1' && eval <= '9' && eval % 2 )
{
return true;
}
else
{
return false;
}
}
| |
It seems that the program is working correctly for the tests the teacher says to do but all of the numbers above 9 also return true and as an odd digit. Is this intended?