Thread: Declared identifier reported as undeclared

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Declared identifier reported as undeclared

    I'm doing an exercise out of a book I've been using (Deitel: C How to Program) and I'm trying to make a program that determines perfect numbers.

    Everything was going well but all the sudden it is saying alot of my variables aren't declare and there is a problem with my function!

    Code:
    exercise3.c:44: error: syntax error before "int"
    exercise3.c:45: error: redeclaration of `check'
    exercise3.c:44: error: `check' previously declared here
    exercise3.c:62: error: `counter' undeclared (first use in this function)
    exercise3.c:62: error: (Each undeclared identifier is reported only once
    exercise3.c:62: error: for each function it appears in.)


    Code:
    #include <stdio.h>
    
    int perfect(int number);
    
    int main()
       {
       int input = 0, factor1, factor2, counter = 2, check = 0;
    
       scanf("%d", &input);
    
       counter = 2;
    
       if ( perfect(input) == 1)
          {
          printf("%d is a perfect number\n", input);
          
          for(factor1 = 2; factor1 <= (input - 1); ++factor1)
             {
             for (factor2 = 2; factor2 <= (input - 1); ++factor2)
                {
                if ( (factor1 * factor2) == input)
                   {
                      if (factor1 != check && factor2 != check)
                      {
    
                         if ( (counter % 2) == 0)                
                            check = factor2;
                         ++counter;
                      printf("%d + %d + ", factor1, factor2);
                      }
                   }
                }
             }
          printf("1 = %d\n", input);
          }
    
     
    
       return 0;
       }
    
    int perfect (int number)
    {
    int factor1, factor2, check = 0, perfect = 0, int counter = 2;
    int check = 2;
    
    for (factor1 = 2; factor1 <= (number - 1); ++factor1)
       {
       for (factor2 = 2; factor2 <= (number - 1); ++factor2)
          {
    
          if ( (factor1 * factor2) == number)
             {
    /* -------------------------------------------------------------------------------------- */
    /* the check needs to be worked out to register at the half way point through             */ 
    /* factoring so numbers aren't counted twice                                              */
    /* -------------------------------------------------------------------------------------- */  
           if (factor1 != check && factor2 != check)
                {
                perfect = perfect + factor1 + factor2;
    
                if ( (counter % 2) == 0 )
                   check = factor2;
                ++counter;
    
                printf(" factor1 is %d\n factor2 is %d\n check is %d\n", 
    factor1, factor2, check);
                printf(" perfect is %d\n", perfect);
    
                }
             }
    
          }
       }
    
       if ( (perfect + 1) == number)
          return 1;
       else
          return 0;
    }

    Does anyone know what the deal is?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int factor1, factor2, check = 0, perfect = 0, int counter = 2;
    int check = 2;
    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
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Notice the syntax error before int check = 2;

    Read that line above int check = 2; carefully.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    int factor1, factor2, check = 0, perfect = 0, int counter = 2;
    int check = 2;
    That int before counter shouldn't be there if you're using commas between declarations. You're redeclaring check on the next line.
    Code:
    int factor1, factor2, check = 2, perfect = 0, counter = 2;
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM