C++ If-else Function Question

Jul 10, 2016 at 5:14am
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?
Last edited on Jul 10, 2016 at 5:15am
Jul 10, 2016 at 5:39am
Hi,

here's some sample output i got using cpp.sh


Enter a character to check if it's odd: 33
The character is an odd digit.



Enter a character to check if it's odd: 43
The character is not an odd digit.


the thing is that your char ch will just store a single char. so it will only evaluate the first digit of the input

so it will just check the (1st) 3 in 33 and the 4 in 43
Last edited on Jul 10, 2016 at 5:41am
Jul 10, 2016 at 5:43am
Oh! That makes perfect sense. Thank you! So the program is working as intended then. Right?
Jul 10, 2016 at 5:49am
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.


so according to assignment its quite right...

but in real life there are better and more efficient way to check for odd even which work with bigger numbers, so stick to them when solving a real life problem... but for practice assignment this is great :)
Jul 10, 2016 at 6:08pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
If - Else example

#include <iostream.h>
#include <math.h> 
int main()
{
double radius;
//get user input
cout<<"Please enter the radius : ";
cin>>radius;
//act on user input
if(radius < 0.0)
cout<<"Cannot have a negative radius"<<endl;
else
cout<<"The area of the circle is "<<3.1416 * pow(radius,2)<<endl;

return 0;
}

http://cplusplus.happycodings.com/code-snippets/code13.html
Last edited on Jul 10, 2016 at 6:09pm
Topic archived. No new replies allowed.