invalid operands to Binary % (have ' float' and ' int')

Asked

Viewed 2,531 times

-1

When I compile the code I get the error in the question title 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);
  }

1 answer

0


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

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

Or changes the notas for whole or perform the operation in the field of floats through the function 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 centimes, and as if everything was multiplied by 100, to avoid precision problems that arise when dealing with floats. This way you won’t need to use fmod and the normal module operator will be sufficient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.