I am having trouble getting my code to compile.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkh 324
    New Member
    • Jul 2010
    • 3

    #1

    I am having trouble getting my code to compile.

    I cannot seem to get my program to compile correctly.
    The error codes I am receiving are:
    "ISO C90 forbids mixed declarations and code"(lines 31,33,35
    and also:
    "Invalid operands to binary %"(lines 33,35)

    the following lines are
    30. [*gallons=usercu ps/GAL;]
    31. [float remgal=usercups %GAL;]
    32. [*quarts=remgal/QRT;]
    33. [float remquart=quarts %QRT;]
    34. [*pints=remquart/PNT;]
    35. [float rempint=pints%P NT;]
    36. [*cups=rempint;]

    GAL, QRT, and PNT are defined before any function is started.

    Please help me understand what I am doing wrong.
  • jkh 324
    New Member
    • Jul 2010
    • 3

    #2
    Update:
    I figured out the mixed declarations and code thing. I listed all declarations at the beginning and have the calculations belowand eliminated that error. However, I am still receiving the "Invalid operands to binary %" error. I do not really understand this error.
    Thank you

    Comment

    • tomthebomb
      New Member
      • Jul 2010
      • 1

      #3
      The % operator is only defined for integers. That is something like
      int a=10;
      int b=7;
      int c=a%b;

      is O.K. but that would fail if a and b were doubles or floats.
      So pints, quarts, PNT and QRT should be declared as ints, or else you should cast them as such.
      Also check out http://bytes.com/topic/c/answers/495...uble-variables
      -Tom

      Comment

      • jkh 324
        New Member
        • Jul 2010
        • 3

        #4
        Update

        I am still having problems. I am still getting the error code "Invalid operands to binary %"

        I thought that I had everything declared correctly.

        The following is the section of code that is giving me problems:

        24.void liquid(int cups1, int *gal1, int *quart2, int *pints3, int *cups4)
        25.
        26. {
        27. int remgal=cups1%GA L;
        28. int remquart=quart2 %QRT;
        29. int rempint=pints3% PNT;
        30.
        31. *gal1=cups1/GAL;
        32. *quart2=remgal/QRT;
        33. *pints3=remquar t/PNT;
        34. *cups4=rempint;
        35. }

        My compiler is giving that error for lines 28 and 29 but not for line 27 so I am confused.
        GAL is defined as 16
        QRT is defined as 4
        PNT is defined as 2

        Wouldn't defining them be just as good as declaring them as ints?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          28. int remquart=quart2 %QRT;
          29. int rempint=pints3% PNT;

          quart2 and pints3 are int*. The % operator does not work with pointers. It works for integer types only.

          Comment

          Working...