I am trying to learn C++ through the book 'C++ Without fear'.
So far I think I have understood everything well.
Now at page 58 there is an exercise to write a program to find out if a given number is a prime number or not.
Below is the code from the book. According to the book it should work, but I keep getting an error when compiling in line number 22. And I just can't figure out what is wrong. I am using DevC++ 4.9.8 as IDE.
First I thought that the declared 'int n' should be a 'double', but that doesn't solve the problem.
I have also tried out the expression 'sqrt(n)' in another set up. In a simple line where I typed. cout << sqrt(n); and it works. In this case I had declared a double for n.
Can somebody give me a hint?
1 #include <iostream>
2 #include <cmath>
3
4 using namespace std;
5
6 int main() {
7
8 int n; // Number to test for prime-ness
9 int i; // Loop counter
10 int is_prime = true; // Boolean flag...
11
12 // Assume true for now.
13 // Get a number from the keyboard.
14
15 cout << "Enter a number and press ENTER: ";
16 cin >> n;
17
18 // Test for prime by checking for divisibility
19 // by all whole numbers from 2 to sqrt(n).
20
21 i = 2;
22 while (i <= sqrt(n)) { // While i is <= sqrt(n),
23 if (n % i == 0) // If i divides n,
24 is_prime = false; // n is not prime.
25 i++; // Add 1 to i.
26 }
27
28 // Print results
29
30 if (is_prime)
31 cout << "Number is prime." << endl;
32 else
33 cout << "Number is not prime." << endl;
34
35 system("PAUSE");
36 return 0;
37 }
And what is the problem? Are you unable to read the compiler messages?
They say that there are at least three functions with the same name sqrt and differenet types of the parameter. As there is no function sqrt with the parameter of type int the compiler does not know which function to select among listed for your argument.
For example you could write the following way
//line 8
double n; // Number to test for prime-ness
Instead of Vlad's suggestion.
Just a small remark on your posting of code, please post it within [code ] ... [/code] blocks and leave out the line numbers, it makes it easier for us to read and copy the code.
[Warning] In function `int main()':
22 parse error before `{' token
At global scope:
30 parse error before `if'
35 ISO C++ forbids declaration of `system' with no type
35 `int system' redeclared as different kind of symbol
373 C:\Dev-Cpp\include\stdlib.h previous declaration of `int system(const
35 invalid conversion from `const char*' to `int'
36 parse error before `return'
C:\Users\Paul\Makefile.win [Build Error] [prime1.o] Error 1
Here is the code. I have implemented the changes you guys have suggested.
Below the code are the messages from the compiler.
1 #include <iostream>
2 #include <cmath>
3
4 using namespace std;
5
6 int main() {
7
8 double n; // Number to test for prime-ness
9 int i; // Loop counter
10 int is_prime = true; // Boolean flag...
11
12 // Assume true for now.
13 // Get a number from the keyboard.
14
15 cout << "Enter a number and press ENTER: ";
16 cin >> n;
17
18 // Test for prime by checking for divisibility
19 // by all whole numbers from 2 to sqrt(n).
20
21 i = 2;
22 while (i <= sqrt((double)n) { // While i is <= sqrt(n),
23 if (n % i == 0) // If i divides n,
24 is_prime = false; // n is not prime.
25 i++; // Add 1 to i.
26 }
27
28 // Print results
29
30 if (is_prime)
31 {
32 cout << "Number is prime." << endl;
33 }
34 else
35 {
36 cout << "Number is not prime." << endl;
37 }
38 system("PAUSE");
39 return 0;
40 }
messages:
[Warning] In function `int main()':
22 parse error before `{' token
At global scope:
30 parse error before `if'
38 ISO C++ forbids declaration of `system' with no type
38 `int system' redeclared as different kind of symbol
373 C:\Dev-Cpp\include\stdlib.h previous declaration of `int system(const
38 invalid conversion from `const char*' to `int'
39 parse error before `return'
C:\Users\Pau\Makefile.win [Build Error] [prime1.o] Error 1
It is very bad code. The first is that prime numbers can be only among integer numbers but you declared n as double. The second if you already know that a number is not a prime number why are the iterations of the loop continuing? The third is why are you defining is_prime as int and assigning to it boolean literal as here
int is_prime = true ?!
#include <iostream>
#include <cmath>
#include <cstdlib>
int main()
{
int n; // Number to test for prime-ness
bool is_prime = true; // Boolean flag...
std::cout << "Enter a number and press ENTER: ";
std::cin >> n;
n = std::abs( n );
for ( int i = 2; is_prime && i <= std::sqrt( ( double )n ); i++ )
{
is_prime = n % i != 0;
}
if ( is_prime ) std::cout << "Number is prime.";
else std::cout << "Number is not prime.";
std::cout << std::endl;
std::system( "PAUSE" );
return 0;
}