Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid operands of type 'double' and 'int' to binary 'operator%'

I'm writing a program for my control structures class and I'm trying to compile it. The only error, at least the only error the compiler is picking up is saying invalid operands of types 'double' and 'int' to binary 'operator%'. Most of the program isn't included since it's too long and doesn't really pertain to this problem, at least I don't believe.

double maxTotal, minTotal;

cin >> maxTotal >> minTotal;

int addCalc;

static_cast<int>(maxTotal);

if(maxTotal % 2 == 1)
     addCalc = minTotal;
else
     addCalc = 0;
like image 393
user2649644 Avatar asked Aug 04 '13 01:08

user2649644


1 Answers

Your static_cast isn't doing anything. What you should be doing is:

if(static_cast<int>(maxTotal) % 2 == 1)

Variables in C++ cannot change types. Static cast returns the casted value it does not change the input variable's type, so you have to use it directly or assign it.

int iMaxTotal = static_cast<int>(maxTotal);

if(iMaxTotal % 2 == 1)
    addCalc = minTotal;
else
    addCalc = 0;

This would work too.

like image 185
Borgleader Avatar answered Sep 23 '22 10:09

Borgleader