Thread: New to C any help would be great.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    New to C any help would be great.

    Hey I am learning to program in C at the moment and I'm having a few problems. I keep getting parse errors when I try to compile as well as a problem with the break statement.

    Any help would be really appreciated. Thanks.

    Code:
    /* Check to see if a number is prime or not.
       A number is prime if it can only be dividied by itself and 1
       but by no other number.
    */
    
    #include <stdio.h>
    
    int main()
    {
       int prime = 1;
       int number;
       int count;
    
       printf("Please enter a number ");
       if (scanf("%d", number) != 1)
       {
          printf("Invalid number entered\n");
          return 1;
       }
       
       for (count=1, count<number, count++);
       {
          if (number % count = 0)
          {
             break;
             prime = 0;
          }
       }
     
       if prime 
          printf("\n$d is prime\n", number);
       else
          printf("\n$d is not prime\n", number);
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You have two parsing errors and a logic error. In your for loop, you are using commas instead of semi-colons. Your last if() has no parantheses. As for the logic error, you are breaking before setting prime to 0; therefore, prime will never be set to 0. Just switch those two lines over and you should be ok.

    PS: You also have $d instead of %d in your two last printf() statements.

    Edit: Here's the code, you also had mixed '=' with '=='.

    Code:
    /* Check to see if a number is prime or not.
       A number is prime if it can only be dividied by itself and 1
       but by no other number.
    */
    
    #include <stdio.h>
    
    int main()
    {
       int prime = 1;
       int number;
       int count;
    
       printf("Please enter a number ");
       if (scanf("%d", number) != 1)
       {
          printf("Invalid number entered\n");
          return 1;
       }
       
       for (count=1; count<number; count++);
       {
          if (number % count == 0)
          {
             prime = 0;
             break;
          }
       }
    
       if(prime)
          printf("\n%d is prime\n", number);
       else
          printf("\n%d is not prime\n", number);
    
       return 0;
    }
    Last edited by Desolation; 08-21-2006 at 05:57 AM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    if (number % count = 0)
    = is the assignment operator. == is the comparison operator.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    the problem with break statement is this that your for ends with ;.
    Code:
    /* Check to see if a number is prime or not.
       A number is prime if it can only be dividied by itself and 1
       but by no other number.
    */
    
    #include <stdio.h>
    
    int main()
    {
       int prime = 1;
       int number;
       int count;
    
       printf("Please enter a number ");
       if (scanf("%d", &number) != 1)
       {
          printf("Invalid number entered\n");
          return 1;
       }
       
       for (count=1; count<number; count+= 2)
       {
          if ((number % count) == 0)
          {
             prime = 0;
             break;
          }
       }
     
       if (prime) 
          printf("\n%d is prime\n", number);
       else
          printf("\n%d is not prime\n", number);
    
       return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (count=1, count<number, count++);
    Gotta love those trailing ; on the end of for loops

    Also, you use ; to separate steps in a for loop, not commas

    Why do I get the feeling this code was deliberately broken, and it was the OP's homework to fix all the syntax errors....
    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.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ahhhhh ok thanks very much for all your help. What is a parsing error? Is it similar to a syntax error?

    Thanks once again.

  7. #7
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Quote Originally Posted by Desolation
    You have two parsing errors and a logic error. In your for loop, you are using commas instead of semi-colons. Your last if() has no parantheses. As for the logic error, you are breaking before setting prime to 0; therefore, prime will never be set to 0. Just switch those two lines over and you should be ok.

    PS: You also have $d instead of %d in your two last printf() statements.

    Edit: Here's the code, you also had mixed '=' with '=='.

    Code:
    /* Check to see if a number is prime or not.
       A number is prime if it can only be dividied by itself and 1
       but by no other number.
    */
    
    #include <stdio.h>
    
    int main()
    {
       int prime = 1;
       int number;
       int count;
    
       printf("Please enter a number ");
       if (scanf("%d", number) != 1)
       {
          printf("Invalid number entered\n");
          return 1;
       }
       
       for (count=1; count<number; count++);
       {
          if (number % count == 0)
          {
             prime = 0;
             break;
          }
       }
    
       if(prime)
          printf("\n%d is prime\n", number);
       else
          printf("\n%d is not prime\n", number);
    
       return 0;
    }
    check your for loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Great Teacher..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-03-2003, 06:40 AM
  2. Whats so great about Unix?
    By manwhoonlyeats in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-23-2002, 09:15 PM
  3. A great C/C++ community
    By Donk in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-15-2002, 08:05 PM
  4. What's so great about C++?
    By InFeStEd-ArCh0n in forum C++ Programming
    Replies: 32
    Last Post: 05-07-2002, 09:49 PM
  5. A great community
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 02-11-2002, 11:16 PM