Not declared in this scope

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 03-08-2009
Taka
Not declared in this scope
Hey if anyone could help me out here I would really appreciate it. I'm not sure what do to to get rid of the below errors/warnings.

prime.cpp: In function 'int main()':
prime.cpp:12: error: 'isPrime' was not declared in this scope
prime.cpp: In function 'int isPrime(int)':
prime.cpp:39: warning: converting to 'int' from 'double'

Code:

``` #include <iostream> using namespace std; #include <math.h>   #define TRUE 1; #define FALSE 0;   int main() {   int 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);   } }   int 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; }```
• 03-08-2009
Codeplug
The compiler doesn't know that "isPrime" is defined later in the file. You need to declare it before using it. Add the following before main:

int isPrime(int);

The compiler will then know that "isPrime" is a function taking/returning int, when it sees it.

gg
• 03-08-2009
Taka
I tried that but it came up with an "unexpected initialiser before int" error.
• 03-08-2009
Taka
Sorry I know what it was I was doing wrong.

Can anyone help me with why it is always printing out:

Please enter a positive number 4

-4195180is a prime number

Updated code:

Code:

```#include <iostream> using namespace std; #include <math.h> int isPrime(int); void getNumber(int); #define TRUE 1; #define FALSE 0; 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);   } } int 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; }```
• 03-08-2009
laserlight
You should use pass by reference instead of pass by value, otherwise the number variable in the main function is never modified by getNumber().

By the way, C++ has a bool type with true and false constants, so it does not make sense to use an int with TRUE and FALSE macros.
• 03-08-2009
Taka
I'm sorry but I don't know what you mean at all.
• 03-08-2009
tabstop
You had it right the first time -- compare the original getNumber with your new one.

Not only should "#define TRUE 1" never appear in C++, given that "true" already exists, it should most definitely not appear with a semicolon at the end. Since isPrime asks a true-false question, you should use a true-false datatype, specifically "bool".
• 03-08-2009
Taka
When I left it as

Code:

` void getNumber(int &number)`
I got a compile error:

prime.cpp: In function 'int isPrime(int)':
prime.cpp:44: warning: converting to 'int' from 'double'
Undefined first referenced
symbol in file
getNumber(int) /var/tmp//cc1BaH0f.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
• 03-08-2009
tabstop
Did you remember to change the prototype too? Otherwise post code.
• 03-08-2009
Taka
Code:

```#include <iostream> using namespace std; #include <math.h> int isPrime(int); void getNumber(int); #define TRUE 1; #define FALSE 0; 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);   } } int 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; }```
• 03-08-2009
laserlight
Basically, this:
Code:

`void getNumber(int);`
should be either:
Code:

`void getNumber(int& number);`
or:
Code:

`int getNumber();`
Then you make the change in the function definition (implementation) as well.

Now this:
Code:

`int isPrime(int);`
should be:
Code:

`bool isPrime(int number);`
then you get rid of these lines:
Code:

```#define TRUE 1; #define FALSE 0;```
Note that if you really did need them, those semi-colons should be removed, as mentioned by tabstop.

So, no more TRUE or FALSE. What to do? Just use true or false.
• 03-08-2009
Taka
So it should be like this?

Code:

```#include <iostream> using namespace std; #include <math.h> int isPrime(int); void getNumber(int& number); #define TRUE 1 #define FALSE 0 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);   } } int 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; }```
• 03-09-2009
laserlight
Quote:

Originally Posted by Taka
So it should be like this?

You're not paying attention, are you? The code that you corrected should be removed. I was merely pointing out how it would be corrected if it were not to be removed.

There is another thing that I did not spot earlier: you have a using directive (using namespace std; ) before a header inclusion. That is bad practice, although it might not matter especially since the header is a standard header. Move the using directive to after the header inclusion.
• 03-09-2009
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.

I've been told I need to leave the True/False statements in there thats why I didn't take them out.
• 03-09-2009
MarkZWEERS
Code:

```#include <iostream> #include <cmath> using namespace std; bool isPrime(int); int getNumber(); int main() {   int number = getNumber();   if (isPrime(number))       cout << "\n" << number << "is a prime number\n";   else       cout << "\n" << number << "is not a prime number\n";   return 0; } int getNumber() {   int number;   cout << "Please enter a positive number ";   cin >> number;   if (!cin.good())   {       printf("Invalid number entered\n");       exit(1);   }   return number; } bool isPrime(int number) {   int count, s;   bool isprime = true;   /* 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)         isprime = false;   }   return false; }```
Include "cmath" , not "math.h".

In main(), affect 'number' by returning the number. This is the most logical.
In the function 'isPrime' you don't have to verify for both 'true' and 'false', just make it true unless otherwise found.
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).
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last