I’m new to this and talking an intro class. I’ve read and reread my course material so far and I can’t get this line to work.
It has to display You have 2 even and 3 odd numbers but no matter what I do, it keeps coming out with 0 even and 0 odd.
Tried used if (number %2==0){ number = even}
If(number %2!=0){ number = odd} but that’s not working. Any suggestions

Dani AI

Generated

The symptom "always 0 even and 0 odd" means the counters never change or get reset each iteration. As and pointed out, writing something like number = even overwrites the input value instead of increasing an even counter. Declare counters (for example, evenCount and oddCount) once before the loop and update those counters inside the loop — do not re-declare them inside the loop body.

Common checks that catch this class of bug:

  • Confirm counters are initialized to 0 before reading numbers and that the final print happens after the loop.
  • Avoid assignment where an increment is intended; use an increment operation on the counter variable.
  • Make sure the if/else covers exactly one branch per number (no accidental second test that leaves both counters unchanged).
  • Verify the input type: the value read into number must be an integer, not a string or char.
  • For parity testing, n % 2 == 0 is fine; as noted, (n & 1) == 0 tests the low bit and is an equivalent, slightly lower-level alternative.

A compact, modern approach (shows the bitwise test and keeps counting logic outside any per-number scope):

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};
    auto evens = std::count_if(nums.begin(), nums.end(),
        [](int x){ return (x & 1) == 0; });
    std::cout << "You have " << evens << " even and "
              << (nums.size() - evens) << " odd numbers\n";
}

For a simple debug strategy, print each input and which branch ran while iterating. That quickly reveals whether the if/else body is ever reached or whether counters are being reset. Use clear names (evenCount, oddCount) to avoid confusing the input variable with counters.

Recommended Answers

All 4 Replies

I guess number is an integer, what are odd and even in your code?
Could you explain please?

Oh, I guess I see now. I think odd stands for the number of odds and even for the number of evens. Right?
Instaed of using number = even, use even++ or even = even +1

I suggest using a bitwise AND to test if the rightmost bit is a 0 or 1.

Just to clarify where your error is...

if you find an even number, you are executing a boolean statement (number = even).
if you find an odd number, you are executing a boolean statement (number = odd).

You are never incrementing even or odd. That is what ddanbe is showing you with even++ or even = even +1.

if (number %2==0)
{
    even++;
}
else
{
    odd++;
}

And, to Reverand Jim's point, the bitwise function is more efficient than the mod function but since you are "new to this" you probably haven't stumbed over bitwise functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.