invalid operands to binary% (have 'float' and 'int') [closed]

-1

When compiling the code I get the error in the title of the question for the line res100=notas%100;

How to solve?

Code:

      float notas,N100,M1,M050,M025,M010,M005,M001,res100,res2,res1,res05,res025,res010,res005,res001,N50,N20,res20,N10,res10,N5,res5,N2;
  scanf("%f",&notas );
  if ((notas>0)&&(notas<1000000.00));
  {
    N100=(notas/100);
    res100=notas%100;
    N50=(res100/50);
    res50=res100%50;
    N20=(res50/20);
    res20=res50%20;
    N10=(res20/10);
    res10=res20%10;
    N5=(res10/5);
    res5=res10%5;
    N2=(res5/2);
    res2=res5%2;
    M1=(res2/1);
    res1=res2%1
    M050=(res1/0.50);
    res050=res1%0.50;
    M025=(res050/0.25);
    res025=res050%0.25;
    M010=(res025/0.10);
    res010=res025%0.10;
    M005=(res010/0.05);
    res005=res010%0.05;
    M001=(res005/0.01);
    printf("NOTAS:\n");
    printf("%.2f nota(s) de R$ 100.00\n", N100);
    printf("%.2f nota(s) de R$ 50.00\n", N50);
    printf("%.2f nota(s) de R$ 20.00\n", N20);
    printf("%.2f nota(s) de R$ 10.00\n", N10);
    printf("%.2f nota(s) de R$ 5.00\n", N5);
    printf("%.2f nota(s) de R$ 2.00\n", N2);
    /*MOEDAS*/
    printf("MOEDAS\n");
    printf("%.2f moeda(s) de R$ 1.00\n%.2f moeda(s) de R$ 0.50\n
    %.2f moeda(s) de R$ 0.25\n
    %.2f moeda(s) de R$ 0.10\n
    %.2f moeda(s) de R$ 0.05\n
    %.2f moeda(s) de R$ 0.01\n",M1,M050,M025,M010,M005,M001);
  }
    
asked by anonymous 15.10.2018 / 23:55

1 answer

0

You can not use the modulo operator (remainder of the entire division), % , between a float and a int which is what happens here:

float notas...;
res100 = notas % 100;
// float --^      ^---int

Either change the notas to integer or perform the floats domain operation through the fmod :

res100 = fmod(notas, 100);
For the code that has to calculate the change, the ideal is to treat everything as integers, as if the unit were centimos, and as if everything were multiplied by 100, to avoid problems of precision that arise when dealing with floats. This way you will not need to use fmod and the normal modulo operator will suffice.

    
16.10.2018 / 00:40