Error: invalid operand to binary %

This is a discussion on Error: invalid operand to binary % within the C Programming forums, part of the General Programming Boards category; I am trying to use the modulus operator to see if the remainder of the result of two numbers being ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    Error: invalid operand to binary %

    I am trying to use the modulus operator to see if the remainder of the result of two numbers being divided is zero and I get the error "invalid operands to binary" on this line of code:
    Code:
    if(prime % div == 0)
    Could someone point me in the direction of my error? I looked up the modulus operator on this site and It wasnt of much help for this problem.

    Oh and I am using Dev-C++

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are prime and div both integral types?
    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
    Jul 2006
    Posts
    111
    no, prime is a double and div is an integer.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well there's your answer then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Ah stupid me. The program executes without errors but It unexpectedly closes. I noticed all the hype around here about checking if a number is prime, so I thought I would try. Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      long div = 2;
      long prime;
      printf("Input the number you wish to chek is prime\n");
      scanf("%d\n", prime);
      for(div == 2; div < prime; div++);
      {
            if(prime % div == 0)
            {
                     printf("%d", prime, " is not a prime number\n");
                     
            }
            else
            {
                     printf("%d", prime, " is a prime number\n");
                     
            }
      }
      printf("Press [ENTER] to continue\n");
      getch();
      return 0;
    }
    Any hints on debugging it would be helpful. Also, how come you cant use floating point variables with this?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by lilrayray
    Any hints on debugging it would be helpful.
    Pick the lint.
    test.c 6 [Warning 578] Declaration of symbol 'div' hides symbol 'div(int, int)' (stdlib.h)
    test.c 9 [Warning 560] argument no. 2 should be a pointer
    test.c 9 [Warning 530] Symbol 'prime' (line 7) not initialized
    test.c 10 [Warning 520] Expected void type, assignment, increment or decrement
    test.c 10 [Info 722] Suspicious use of ;
    test.c 14 [Info 719] Too many arguments for format (1 too many)
    test.c 19 [Info 719] Too many arguments for format (1 too many)
    test.c 24 [Info 718] Symbol 'getch' undeclared, assumed to return int
    test.c 24 [Info 746] call to function 'getch()' not made in the presence of a prototype
    test.c 26 [Info 715] Symbol 'argv' (line 4) not referenced
    test.c 26 [Info 818] Pointer parameter 'argv' (line 4) could be declared as pointing to const
    test.c 26 [Info 715] Symbol 'argc' (line 4) not referenced
    test.c 27 [Info 766] Header file 'stdlib.h' not used in module 'test.c'
    Quote Originally Posted by lilrayray
    Also, how come you cant use floating point variables with this?
    Because you can't. Use fmod for floating point modulus (if that is indeed what you need).
    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.*

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    what compiler are you using, Mine returns no errors. Sorry if I seem unknowledgable, but I am having much trouble understanding these errors. Any recources on dealing with some of these errors would be appreciated

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what compiler are you using, Mine returns no errors.
    Lint isn't a compiler, it's a secondary tool that performs a more thorough error and portability test than compilers do. If you want to survive this forum, lint is generally a good tool to use liberally.

    >but I am having much trouble understanding these errors
    Don't worry about it. We won't claim that the errors are comprehensible to begin with.

    >Any recources on dealing with some of these errors would be appreciated
    You can find plenty of useful information (as well as a good lint) at http://www.gimpel.com/.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Hey thanks. The interactive demo is pretty cool. I think Ill wait on buying the real thing. Anyway, I have been trying to tweak the code, but I havent had any luck. What is all this about pointers initialization?

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Hmm. When I remove the scanf statement, it runs fine, just doesnt do what I want it to. I tried using I beleive fgets() but it still has the same unexpected closing.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > for(div == 2; div < prime; div++);
    > test.c 10 [Info 722] Suspicious use of ;
    1. How many times does this loop run?
    2. What work is performed by this loop when it is run?
    3. Will removing the final ; change that?

    > printf("%d", prime, " is not a prime number\n");
    > test.c 14 [Info 719] Too many arguments for format (1 too many)
    The , is not some kind of string concatenation in printf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Actually, I changed both of these errors last night. It has to be an input problem. If I remove the scanf statement and then set a value to prime, it runs perfectly.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Post your latest code.

    For input, use fgets() and sscanf() as described in various FAQs
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Here is what I have so far (wihtout sscanf or fgets()):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      long divi = 2;
      long prime;
      int check = 0;
      printf("Input the number you wish to chek is prime\n");
      sscanf("%d\n", prime);
      for(divi == 2; divi < prime; divi++)
      {
            if(prime % divi == 0)
            {
                     check = check + 1;
                     
            }
            else
            {
                     check = check + 0;
                     
            }
      }
      if(check > 0)
      {
               printf("not a prime number.  Number of factors: %d\n", check+2);
      }
      else
      {
               printf("prime number. Number of factors: %d\n", check+2);
      }
      printf("Press [ENTER] to continue\n");
      getch();
      return 0;
    }
    I had already tried fgets() but got the same problem. sscanf is for converting a string to an integer correct? How would I use that?
    Last edited by lilrayray; 07-30-2006 at 08:04 AM.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > for(divi == 2;
    It's a good job you already assigned a value, because this does nothing - you used == where = is more usual.

    Code:
    char buff[100];
    fgets ( buff, sizeof buff, stdin );  /* read a whole line */
    sscanf ( buff, "%ld", &prime );      /* extract one long int */
    > check = check + 0;
    You may as well do without the else part at all.

    > getch();
    Use getchar()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 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. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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