Not declared in this scope

This is a discussion on Not declared in this scope within the C++ Programming forums, part of the General Programming Boards category; Try not to use 'return' in loops or functions, but affect a local variable which you return always in the ...

  1. #16
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Try not to use 'return' in loops or functions, but affect a local variable which you return always in the end (although here it doesn't really matter).
    Of course it matters - now you return false even if the number is prime
    you do not check 2 to be a divider
    you check all numbers even if the 3 is a divider

    so better stop fixing functions in this way - you enter too many bugs
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Taka
    I'm sorry I'm trying to understand what you are saying but I am only learning C++ and I'm not really getting what you are trying to tell me.
    Okay, which part would you like me to explain further?

    Quote Originally Posted by Taka
    I've been told I need to leave the True/False statements in there thats why I didn't take them out.
    If those are the requirements of your instructors, then so be it, but be aware that your instructors are teaching you to do something that is unnecessary.

    By the way, this statement is not quite correct, since 2 is the even prime number: "Every even number is not prime". Not only that, but it looks like you wrote isPrime() as if it were isComposite().

    Quote Originally Posted by MarkZWEERS
    Try not to use 'return' in loops or functions, but affect a local variable which you return always in the end (although here it doesn't really matter).
    I do not subscribe to the notion of strict "single entry, single exit", and as vart pointed out, you did not practice what you preached since you returned false instead of isprime at the end. Furthermore, without a break, you potentially loop far more than necessary than with a version that uses an immediate return.
    Last edited by laserlight; 03-09-2009 at 06:00 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    so better stop fixing functions in this way - you enter too many bugs
    sorry, I was too rapid in that..

  4. #19
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I'm not understanding the
    Code:
     int& number
    part.

    Wouldn't I need to put it as
    Code:
     int &number
    and then wouldn't I need a
    Code:
     *number
    somewhere in the code?

    I played around with the bool in the code. I'll take it out when I submit but it does make it cleaner and clearer.

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    bool isPrime(int);
    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, s;
    
       /* Every even number is not prime */
       if (number % 2 == 0) return true;
    
       /* 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 true;
       }
       return false;
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Taka
    I'm not understanding the
    Code:
     int& number
    part.

    Wouldn't I need to put it as
    Code:
     int &number
    and then wouldn't I need a
    Code:
     *number
    somewhere in the code?
    Have you learnt about pass by reference yet? The spacing has no meaning in this case, because either way it says that the parameter named number is a reference to an int, so whatever int you pass as an argument to the function can be modified from within the function. This use of & is different from its use as the address of operator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    We haven't gone into it alot yet which is probably why I'm struggling to understand it.

    It compiles without the & but when I try to compile it with it, it comes back with:

    prime.cpp: In function 'bool isPrime(int)':
    prime.cpp:42: warning: converting to 'int' from 'double'

    Undefined first referenced
    symbol in file
    getNumber(int&) /var/tmp//ccy5ogNG.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The compiler is complaining because sqrt() returns a double, but you put its return value into an int without explicitly casting.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Yeah I'm working on fixing that, but then the main error is that it doesn't create the output because of the
    Code:
     getNumber(int&)
    so have I written this incorrectly?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    bool isPrime(int);
    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, s;
    
       /* Every even number is not prime */
       if (number % 2 == 0) return true;
    
       /* 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 true;
       }
       return false;
    }

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Compiling your latest code with gcc-mingw-3.4.5, it gives no errors, just the warning about converting double to int without cast.

    The program runs, but it does not recognise valid primes, such as 31 or 17 as primes, so you probably have a bug in your "is this a prime" function.

    --
    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.

  12. #27
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    OK I will check my compiler. I am using "g++ -lm"

    I'm not very good at math so any hints on where my logic is wrong in isPrime?

  13. #28
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Your "isPrime" function is wrong. 2 is a prime number (and the only even one if I recall correctly), yet you have a line of code where you do something like: If number%2 == 0, return true. So you're basically saying every even number will return true.

    Didn't check the rest since that already seems wrong.

  14. #29
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Quote Originally Posted by Litz View Post
    Your "isPrime" function is wrong. 2 is a prime number (and the only even one if I recall correctly), yet you have a line of code where you do something like: If number%2 == 0, return true. So you're basically saying every even number will return true.

    Didn't check the rest since that already seems wrong.
    It works for 2:
    ./a.out
    Please enter a positive number 2

    2is a prime number

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and what happens if you enter 4?

    --
    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.

Page 2 of 4 FirstFirst 1234 LastLast
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, 12: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, 08:45 PM

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