-3
#include <stdio.h>

int main(void) 
{
    float with;
    float inacbal;
    float acleft;
    scanf("%f",&with);
    scanf("%f",&inacbal);
    if((with%5)==0)//error here
    {
        acleft=inacbal-with-0.50;
        printf("%f",acleft);
    }
    else
        printf("%f",inacbal);
    return 0;
}
lurker
  • 56,987
  • 9
  • 69
  • 103
Dumb
  • 43
  • 1
  • 1
  • 9

2 Answers2

5
float with;
if((with%5) == 0)

is incorrect. You can apply % only to integers. If you really want to do a modulo operation on float, then use fmod or if you're not bothered about the sign of the remainder, then use the new IEEE 754r mandated C99's remainder. From Sun's Numerical Computation Guide:

The remainder(x,y) is the operation specified in IEEE Standard 754-1985. The difference between remainder(x,y) and fmod(x,y) is that the sign of the result returned by remainder(x,y) might not agree with the sign of either x or y, whereas fmod(x,y) always returns a result whose sign agrees with x.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Then how I an check the remainder of it – Dumb Sep 18 '14 at 13:50
  • 1
    @Dumb does `with` have to be a float? Can it be an `int`? If it needs to be a float, then look at `fmod`. – lurker Sep 18 '14 at 13:51
  • +1 `with` can be an integer and the whole problem would go away. – legends2k Sep 18 '14 at 13:53
  • Actually in order to avoid typecasting in this line acleft=inacbal-with-0.50;.I am putting all variables in float otherwise there will be some precision error – Dumb Sep 18 '14 at 13:58
  • Use the right data types that makes sense for what quantity the variable denotes; then type case only when you need to. Say you could do `acleft = inacbal - (float) with - 0.5f;`. – legends2k Sep 18 '14 at 14:01
  • @Dumb Making `with` a `float` instead of an `int` if it really is integer does not help with precision in the `aclef = inacbal - with - 0.50` calculation. A certain amount of precision error will occur with `float` regardless. – lurker Sep 18 '14 at 14:04
  • If I"LL just put float acleft and other in integers then won't there be any precision loss? – Dumb Sep 18 '14 at 14:10
  • If you're measuring quantities which can be only integers (discrete) then use `int` e.g. would be number of _people_, _keys on a keyboard_, _columns on a terminal window_, _files on a machine_, etc. If you want to measure quantities that can be fractions (continuous) then use `float` (or `double` depending upon the precision you want) e.g. _volume_, _length_, _money_, etc. – legends2k Sep 18 '14 at 14:17
1

You are getting that error because you can't use the modulus operator (%) with float.

If you want to calculate the remainder of it,use fmod() like this:

fmod(with,5);

fmod will return the remainder of the division. Don't forget to include math.h in order to use fmod.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83