error: invalid operands to binary %

This is a discussion on error: invalid operands to binary % within the C Programming forums, part of the General Programming Boards category; Hi i'm getting this error when i compile this code: Code: #include <math.h> #include <stdio.h> int main(void) { float num; ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    20

    error: invalid operands to binary %

    Hi i'm getting this error when i compile this code:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main(void)
    {
    float num;
    int total;
    
    num = pow(2,1000)
    total=0
    while (num != 0)
    {
    total=total+num%10
    num=num/10
    }
    
    printf("%d", total);
    }
    what is wrong with num being used for divisible by 10?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You can't use &#37; on a float.
    The operands of the % operator shall have integer type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    then wat should i use to make a float or a double divisible by a certain number?
    casting it to an int would make the program work but give me the wrong value.

    How would u have a double or a float divisible by a certain number?

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Use fmod()
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    thanks man, but wat would be the 1st and 2nd parameters since it asks for them?
    num as the 1st and 10 as the 2nd as when i tried that it gives me:
    undefined reference to 'fmod' collect 2: ld returned 1 exit status.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    The first is the numerator and the second is the denominator. Use google to get actual documentation. Remember google is your friend !!
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    I did, it just said numerator and dominator, but im not sure what would be the denomiator would be

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    n/d => n=numerator d=denominator
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    so then i would just do num/10 then?
    that would make it divisible by ten? as that just sounds like it would be
    num divided by 10, instead of num divisible 10

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    i can't use anything anything for that?

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You can use the following:
    Code:
    float num;
    int remainder;
    num = 12345.6789f;
    remainder = floor(num - (int)( ( num / 10 ) * 10 ));
    It should work for positive floats... and you might like round better, instead of floor.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    Thanks bud and everyone else for ur help! That absolutely worked!

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by OxKing033 View Post
    then wat should i use to make a float or a double divisible by a certain number?
    If x and y are floats, and x / y is an integer (or extremely close to one), then x is divisible by y. You could you fmod() too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with this tutorial program: invalid operands to binary( - )
    By convenientstore in forum C Programming
    Replies: 8
    Last Post: 06-08-2009, 12:53 PM
  2. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 12:11 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. misc.c:3210: invalid operands to binary +
    By Unregistered in forum Linux Programming
    Replies: 2
    Last Post: 07-19-2002, 07:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21