Don't understand if (number % 2 == 0)

Sep 22, 2016 at 5:08am
Just started my first programming class, so I'm a complete newbie.

I have a question about something in one of my programs. It executes fine, but I was wondering why in my if statement does if (number % 2 == 0) allow the output to display that a number is even, when 0 means false? I'm obviously not grasping something.

// Program indefinitely displays an even
// or odd number until a negative number
// is entered.
#include <iostream>
using namespace std;

int main()
{
int number;

cout << "Please enter a number: ";
cin >> number;

while ( number >=0 )
{
if ( number % 2 == 0 )
{
cout << number << " is even \n";
cout << "Please enter a number: ";
cin >> number;
}
else
{
cout << number << " is odd \n";
cout << "Please enter a number: ";
cin >> number;
}
}

cout << "Thank you. \n";
return 0;
}
Last edited on Sep 22, 2016 at 5:09am
Sep 22, 2016 at 5:24am
Actually, I think my problem is with while (number >= 0), correct?
Sep 22, 2016 at 5:29am
> I think my problem is with...
¿what problem?

> when 0 means false?
you've got foo==0, that will be true when foo is 0
Sep 22, 2016 at 5:32am
Even if I change 0 to 1, the output still displays it's an odd number.
Sep 22, 2016 at 7:30am
I've figured out what my problem was. I was reading if (number % 2 == 0) as it equaling to false, not 0 == 0, which is true.
Sep 22, 2016 at 9:18am
There is also operator precedence that can affect results. Whenever there is:
A op1 B op2 C
it might be (as in your case) equivalent to:
(A op1 B) op2 C
but if the op1 and op2 have different priority / order of evaluation, the expression is actually:
A op1 (B op2 C)

In your case the number % 2 evaluates to either 0 or 1.
You apparently know that 0 can convert implicitly to false and the 1 to true.
If num%2 is 1 whenever num is odd and 1 converts to true, then num%2 (in condition) evaluates to true whenever num is odd.
That allows:
1
2
3
4
5
6
if ( num % 2 ) {
  // odd
}
else {
  // even
}


You do repeat code. Furthermore, if the user does not enter a number (i.e. types text), then the input will fail and you have an infinite loop. Luckily, the state of cin converts to bool too and cin >> number does return the cin.
Therefore:
1
2
3
4
5
std::cout << "Enter number:"
while ( std::cin >> number && 0 <= number ) {
  // use number
  std::cout << "Enter another number:"
}


PS. Please use code tags and intuitive indentation when posting code.
Sep 22, 2016 at 10:27am
This should work how you want it to:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

void getInput(int& number)
{
    std::cout << "Please enter a number: ";
    std::cin>> number;

    while ((cin.fail()) || (number <= 0)) // This while loop checks for bad input and
    {                                     // and asks user to re-enter number
        std::cout << "Enter a positive number: ";
        std::cin.clear();
        std::cin.ignore(1000000, '\n');
        std::cin >> number;
    }
}

void isNumOddEven(const int& number, const bool& oddEven)
{
    if (number >= 0)
    {
        if (oddEven) // "oddEven" evaluates to true or false
        {                     // Same as (number % 2) == 0
            std::cout << number << " is even \n";
            std::cout << "Please enter a number: ";
            std::cin >> number;
        }

        else
        {
            std::cout << number << " is odd\n";
            std::cout << "Please enter a number: ";
            std::cin >> number;
        }
    }
}

int main()
{
    int number;
    bool oddEven = ((number % 2) == 0); // Checks if number is odd or even

    getInput(number) // Ask user to enter number
    isNumOddEven(number, oddEven); // Evaluates if number is odd or even

    std::cout << "Thank you.\n";

    return 0;
}
Last edited on Sep 22, 2016 at 10:41am
Sep 22, 2016 at 11:28am
keskiverto, I was trying to input the code correctly, but I couldn't get it to work (first time posting). It kept saying something about java. The preview wasn't showing anything either.

boost lexical cast, thank you, but the way you write code isn't how we're doing it in class.

My main confusion was with if( number % 2 == 0 ) and why it displayed an even number when 0 was used. I was hung up on it meaning that number % 2 is false, when it means 0==0, which is true, therefore it displays an even number. Once I realized that, it all made sense.
Last edited on Sep 22, 2016 at 12:11pm
Topic archived. No new replies allowed.