# help wit functions

• 03-28-2003
Nikisha
help wit functions
can someone help me out wit this?

the question goes somthg like this:

write a fuction int (or ool) IsOddDigit (char ch) that takes a char (ch) and retruns a true if ch is one of the alphabetic digits '1', '3', '5', '7', or '9', otherwise false. write a main() function to drive IsOddDigit() and test its return alue, printing an appropriate message. Include the character you are testing in the message, e.g., print somthg like the following, either:

Code:

```The character '1' is an odd digit. or The character 'a' is not an odd digit.```
• 03-28-2003
HaLCy0n
Take Salem's suggestion and show us what you come up with. Then we can help you from there.
• 03-28-2003
Stoned_Coder
whats the value of this expression (x & 1) when x is even.Whats the value of it when x is odd. Why?
• 03-28-2003
Nikisha
well thnx so much for the hints n all but the thing is tht im totally confused wit this fuctions...we just started tht last week in class n the example my teacher gave me is totally different from this assignment question...so thts my problem
can neone start from scratch wit me plz?
• 03-28-2003
Nikisha
has this got somthg to do wit ASCII?
• 03-28-2003
it doesn't have to be about ASCII but it could be if you want. It could also be about modulus, division, bit twiddling, etc., if you want.

The ASCII value for a given char will be an int. All ints are either even or odd numbers. % can be used to determine if an int is even or odd. But this is only one way to tackle this problem. There are many others, some more sophisticated, some less.
• 03-28-2003
DougDbug
You are going to need a "Function Prototype", and a "Function Definition"

You're going to need to know how to "call" your function from main.

You're going to need an "if" statement.

You could leave out the "if" (odd or even test) at first, and just write a function that returns a number (i.e return 1; ) to main. Then you can add the odd-even test.

The ASCII stuff has to do with the fact that an ASCII 1 (what you are calling "alphanumeric digit", or '1' in C++) is NOT the same as the integer 1.

Your test could take advantage of the fact that the ASCII values are in numerical sequence... the character being tested should be greater-than-or-equal-to the ASCII value of "1' and less-than-or-equal-to the ASCII value of '9'. Maybe that has something do do with your teacher's example (?)
• 03-28-2003
Jamsan
Code:

```#include <iostream.h> bool IsOddDigit (char ch); int main (void) {         char ch;                 bool temp;         cout << "Enter a number from 1-10: ";         cin >> ch;         temp = IsOddDigit (ch);                 // if true, output ODD, else output EVEN         return 0; } bool IsOddDigit (char ch) {         //Do stuff here, and return true if ODD, return false if EVEN }```
outline really of how it should be
• 03-28-2003
DougDbug
Good start....
Just quickly looking (I didn't test-compile), it looks like doing this should make it compile:
Code:

```bool IsOddDigit (char ch) {         //Do stuff here, and return true if ODD, return false if EVEN return 0;          // Function needs to return a bool }```
Then you need to figure out the "stuff"
Code:

```if(something)     return TRUE; else     return FALSE;```
And in main, something like this:
Code:

```temp = IsOddDigit (ch);          // if true, output ODD, else output EVEN if(temp)    // If temp is anything other than zero     cout << "ODD"; else     cout << "EVEN";         return 0;```

NOTES:
if(temp) is the same as if(temp != 0)

It might seem confusing ritht now, but you can leave out the temp variable and just put if(IsOddDigit(ch) ) That will stick the value returned by IsOddDigit() into the if statement... See?
• 03-30-2003
Nikisha
thnx for all ur suggstions..:)

i wrote my program..keeping all ur thoughts in mind
but the problem now is tht no mattter wht value i input, my program keeps returning it as an odd digit
here's my program:

Code:

```#include <iostream> using namespace std; bool IsOddDigit (char ch); int main () {         char ch;         bool temp;         cout << "Enter a number between 1- 10: " << flush;         cin  >> ch;         temp = IsOddDigit (ch);         if (temp != 0)         {                 cout << "The character " << ch                         << " is an odd digit." << endl;         }         else         {                 cout << "The character " << ch                         << " is not an odd digit." << endl;         }                         return 0; } bool IsOddDigit (char ch) {         if (ch <= 1)         {                 return true;         }         else         {                 return false;         } }```
can u help me figure this out?
:(
• 03-30-2003
CheesyMoo
Your method returns true if it is less then or equal to 1, that won't do what you want it to. Remember the % operator is very useful.
• 03-30-2003
Nikisha
ok so i inserted % in there
thnx it works now!:)