GCC Compiler Error on Binary operands

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shantanu Godbole
    New Member
    • Nov 2008
    • 1

    GCC Compiler Error on Binary operands

    temp1[i] = number%pow(10,i )/pow(10,i-1);

    An error is shown in this line even though i include math.h library and use the correct operators

    ERROR: invalid binary operands to %
    How to deal with it?
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    The operands of % must be of type int. Are you sure they're integers? For one, pow() returns a double....

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by Shantanu Godbole
      temp1[i] = number%pow(10,i )/pow(10,i-1);
      You have two operations (% and /). What order do you want them to occur in? Don't spend 5 minutes looking up the C order-of-operations rules; spend 30 seconds adding parentheses.

      I'm going to a guess: you're seeking to solve
      temp1[i] = number % (pow(10,i)/pow(10,i-1));
      [arnaudk is write: this expression is still illegal because of non-int argument.]

      Write that out with pencil and paper as an arithmetic problem rather than as a coding problem. Look for ways to simplify the expression.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Shantanu Godbole
        temp1[i] = number%pow(10,i )/pow(10,i-1);
        If you are trying to conver an integer to a string, this is not how you do it.

        To get the value of a decimal position, do this:

        (theInteger / value) %10 + '0'

        where value is 100 or 1000 etc. The '0' converts the integer result to an ASCII character between 0 and 9 and it's that that goes in the string.

        Comment

        Working...