Thread: help wit functions

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Unhappy 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.

  2. #2
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    Take Salem's suggestion and show us what you come up with. Then we can help you from there.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    whats the value of this expression (x & 1) when x is even.Whats the value of it when x is odd. Why?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    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?

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    has this got somthg to do wit ASCII?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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 (?)

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    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

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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?
    Last edited by DougDbug; 03-28-2003 at 07:09 PM.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    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?

  11. #11
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    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.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    ok so i inserted % in there
    thnx it works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM