modulus error

1
2
        double c = y[k];
        if (c % 2 == 1)


This gives the error message "error: invalid operands of types 'double' and 'int' to binary 'operator%'|"

what does this even mean?
The % operator is not allowed on floating-point values. Either change the type to an integer, or use std::fmod.
Last edited on
what does this even mean
% is an operator
A binary operator takes two operands, in your example, they are c and 2.

The message is saying that both of these should be integers. For example, 3 % 2. However your code uses a double and an integer (for example 3.14 % 2) which is not allowed.

This would compile:
1
2
    int c = y[k];
    if (c % 2 == 1)
but it may not make sense - for example if y is an array of doubles, the conversion will lose the non-integer portion, so for example if y[k] had the value 3.14 or even 3.99, the value would be truncated to just 3 when storing it in int c.
Last edited on
Registered users can post here. Sign in or register to post.