2

im trying to use the % operator on a double in c++, i have done the same in java and it works fine.

is there something im missing here or is this not allowed, sorry im new to c++ so might be making a really stupid error here

    double i =  full_price_in_pence / 100.0;
    double j = full_price_in_pence % 100;
    int final_pounds = (int) i;
    int final_pence = (int) j;

and these are both double values

full_price_in_pence
full_price_in_pounds
AngryDuck
  • 3,931
  • 10
  • 54
  • 88

4 Answers4

9

You should use the std::fmod() function from the <cmath> Standard header:

#include <cmath>

// ...

double j = fmod(full_price_in_pence, 100);
Andy Prowl
  • 119,862
  • 22
  • 374
  • 446
3

% is for integers only, you're looking for fmod.

Femaref
  • 59,667
  • 7
  • 131
  • 173
2

You cannot use % operator for a double variable. Only int variables are allowed to do that.

You can check some good answers from another question like this; you can find them here.

Community
  • 1
  • 1
George Netu
  • 2,730
  • 4
  • 27
  • 49
1

No, it's not allowed. Operands of the % operator must be of integral types. Use std::fmod() instead.