Thread: Not declared in this scope

  1. #31
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ah ok... Any suggestions on what it should be? Im not sure how to calculate it at all.

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since 2 is a special case, you could check first if the number is two (if so, it's a prime so return whatever says "this is a prime"). For all other numbers, try dividing by 2, then use the loop.

    I do believe your loop is wrong too - check your logic [I take it you understand the PRINCIPLE of checking if a number is prime?].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I don't really get the principle of prime numbers sorry.

  4. #34
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Ok so, a prime number is always uneven, unless it's 2.

    First, check if the number is 2. If it is, return true.

    Second, check if the number is even. If it is, return false.

    Lastly, if it's an uneven number, I assume something like this would work:

    Imagine n is the number you want to check.
    Start a cycle with i = 2 for example.
    Check if i*i < n. If it is, check if n%i equals 0. If it does, return false.
    Increment 1 to i.

    If your number survives through this cycle, n is a prime number.

  5. #35
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry guys I realise I must be painful to try and help so thanks for trying.

    This is my latest try, I think I am getting closer at least.

    Code:
    bool isPrime(int number)
    {
       int count, s;
       bool isprime = true;
    
       /* Every even number is not prime */
       if (number == 2) return true;
    
       if (number % 2 == 0) return false;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0)
          isprime = false;
       }
       return isprime;
    }

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks good. To improve performance a bit, you could add a break to the "isPrime = false;" statement, as if you find out that the number isn't a prime, you know the result then and there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #37
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Thanks Mat, I'll have a look into that.

    Just one last issue (though I seem to be full of them) I am trying to submit and I am getting an error "ERROR - Unable to find pointers being used in getNumber function"

    The program looks to work otherwise so I was just wondering where the pointer should be and why I should need it?

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    bool isPrime(int);
    void getNumber(int&);
    
    int main()
    {
       int number;
    
       getNumber(number);
    
       if (isPrime(number))
          cout << "\n" << number << "is a prime number\n";
       else
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int &number)
    {
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    bool isPrime(int number)
    {
       int count;
       double s;
       bool isprime = true;
    
       /* Every even number is not prime */
       if (number == 2) return true;
       if (number % 2 == 0) return false;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0)
          return false;
       }
       return isprime;
    }

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I do not know what the problem is with that code. It looks fine to me.

    What tool are you getting the error from?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #39
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    It is the submission system for my teacher. So he doesn't have to look at it if things don't pass the tests.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Apparently you need to use pointers to implement getNumber(), but I cannot see why unless this is C instead of C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    So I'm really lost. I'm trying to convert my code so that it passes the pointer in getNumber test but I just can't seem to work it out. Any help would be appreciated...

    g++ -lm prime.cpp
    prime.cpp: In function 'int main()':
    prime.cpp:13: error: invalid conversion from 'int' to 'int*'
    prime.cpp:13: error: initializing argument 1 of 'void getNumber(int*)'
    prime.cpp:15: error: invalid conversion from 'int' to 'int*'
    prime.cpp:15: error: initializing argument 1 of 'bool isPrime(int*)'
    prime.cpp: In function 'bool isPrime(int*)':
    prime.cpp:41: error: ISO C++ forbids comparison between pointer and integer
    prime.cpp:42: error: invalid operands of types 'int*' and 'int' to binary 'operator%'
    prime.cpp:45: error: cannot convert 'int*' to 'double' for argument '1' to 'double sqrt(double)'
    prime.cpp:48: error: invalid operands of types 'int*' and 'int' to binary 'operator%'

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    bool isPrime(int *number);
    void getNumber(int *number);
    
    int main()
    {
       int *number;
    
       getNumber(*number);
    
       if (isPrime(*number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int *number)
    {
       cout << "Please enter a positive number ";
       cin >> *number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    bool isPrime(int *number)
    {
       int count;
       double s;
       bool isprime = true;
    
       /* Every even number is not prime */
       if (number == 2) return true;
       if (number % 2 == 0) return false;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) 
          return false;
       }
       return isprime;
    }

  12. #42
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Pointer Errors:
    The first error is in the way your passing the numbers.
    You've defined:
    Code:
    bool isPrime(int *number);
    which essentially means your declaring a pointer to a number.
    The value you pass would have to be the address of that number, not the number itself (the value of a pointer is an address).
    What your doing is passing the value of number by de-referencing it ( * ) which retrieves the value stored at the address.

    If you want to keep the parameter as int * number than you would have to call the function using
    Code:
    isPrime(number)
    which actually means pass the value stored in the pointer number (address of the actual number)
    and assign that to the pointer number in your function.

    A more feasible answer would be passing by reference.

    change:
    Code:
    isPrime(int * number )
    to
    Code:
     isPrime(int & number)
    This will allow you to just use number as if it were an int, so you can remove all the de-referencing within the functions. ie line 3 in getNumber() would be just number instead of *number
    This applies to your getNumber() function as well.

    Tutorials on Pointers:
    http://www.cplusplus.com/doc/tutorial/pointers.html : very complete but can be daunting
    http://www.cprogramming.com/tutorial/lesson6.html : much simpler explanation but doesn't explain the & (address of) operator
    TypeCasting:
    The other error is the type-cast from double to int.
    sqrt returns a double and your storing that data into an int.
    Code:
    bool isPrime(int &number)
    {
       int count;
       int s;
       bool isprime = true;
    
       /* Every even number is not prime */
       if (number == 2) return true;
       if (number % 2 == 0) return false;
    
       /* check every odd number up to the square root of the number */
       s = static_cast<int>( sqrt(number) ); // this line could also be s = (int)(sqrt(number))
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) 
          return false;
       }
       return isprime;
    }
    a whole tutorial on type-casting is available here: http://www.cplusplus.com/doc/tutorial/typecasting.html

    Edit:
    Another note about the passing parameters, as laserlight said, why are you passing the pointers? generally with small(er) datatypes such as int,double,float,char you can just pass them. This is useful because if you call isPrime(7) will result in an error when using pointers because 7 does not have an address. (not a variable just a value)
    Although it is good practice to pass things by reference or by using pointers when using functions, generally this is only necessary for larger objects such as entire classes, structs, or arrays (that includes strings)
    Last edited by balulu; 03-12-2009 at 06:49 AM.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... looking at the error message from your teacher's tool, it appears that you only need to use a pointer in the getNumber() function. This is easy, because the change from using a reference to using a pointer parameter is trivial, since a reference can be seen as a const pointer without pointer syntax. As such:
    Code:
    void getNumber(int* const number)
    {
       cout << "Please enter a positive number ";
       cin >> *number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    The rest of your program would remain the same, except that instead of calling getNumber() as:
    Code:
    getNumber(number);
    You would call it by passing the address of number:
    Code:
    getNumber(&number);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Shouldn't I call getNumber as getNumber(*number)?

    In function 'int main()':
    prime.cpp:13: error: cannot convert 'int**' to 'int*' for argument '1' to 'void getNumber(int*)'


    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    bool isPrime(int &number);
    void getNumber(int* const number);
    
    int main()
    {
       int *number;
    
       getNumber(&number);
    
       if (isPrime(*number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int* const number)
    {
       cout << "Please enter a positive number ";
       cin >> *number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    bool isPrime(int &number)
    {
       int count;
       double s;
       bool isprime = true;
    
       /* Every even number is not prime */
       if (number == 2) return true;
       if (number % 2 == 0) return false;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) 
          return false;
       }
       return isprime;
    }

  15. #45
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       int *number;
    
       getNumber(&number);
    should not be int *number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM