error with while (i <= sqrt(n))

Hi,

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 }

Compilers shows some messages when issue an error/ Is it difficult to you to show the error text or we must guess what error did occur?!
Last edited on
n should be double.

http://cplusplus.com/reference/clibrary/cmath/sqrt/
(i just read it too)
sqrt is an overloaded function. But I wonder is it so difficult to report the error message?!
Hi,

First of all, thanks you all for your quick reply. I appreciate this very much.

Below I have copied the messages in the compiler.
To be honest, I don't really know what they mean yet, but I will find out as I learn more.


[Warning] In function `int main()':
22 call of overloaded `sqrt(int&)' is ambiguous
163 C:\Dev-Cpp\include\math.h candidates are: double sqrt(double)
465 C:\Dev-Cpp\include\c++\cmath long double std::sqrt(long
461 C:\Dev-Cpp\include\c++\cmath float std::sqrt(float)
C:\Users\Paul\Makefile.win [Build Error] [prime1.o] Error 1




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

i <= sqrt( ( double )n)
Last edited on
Hi Vlad,

I am trying to understand what all this means.

I have typed the line you have proposed, but it has no effect.
It gives more error messages.

Anyway, thanks for helping me
Show the code where you get more errors.
closed account (o3hC5Di1)
Hi there,

You could also try
1
2
//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.

All the best,
NwN
Here is 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

closed account (o3hC5Di1)
Hi there,

You need curly braces:

1
2
3
4
5
6
7
8
9
//line 31
if (is_prime)
{
    cout << "Number is prime." << endl;
}
else
{
    cout << "Number is not prime." << endl;
}


And line 22 should look like this if you used Vad's suggestion:

while (i <= sqrt((double)n)) {


All the best,
NwN
@Paul54
Here is the code:



And where is the code?!
This doesn't have any effect either.

I have made a screenshot.
How can I attach it to this message?
closed account (o3hC5Di1)
Unfortunately you can't.

Can you please post you full current code again (your program) as well as the last compiler errors?

Thanks,
NwN
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
closed account (o3hC5Di1)
Hi there,

Change line 22 to:
 
while (i <= sqrt(n)) { // While i is <= sqrt(n),   a ')' was missing here 

And add #include <stdlib.h> on line 3.

All the best,
NwN
Last edited on
Great!!!!

It works. I see what was wrong.
I have learned a lot.

Thanks !!
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 ?!

The better code could look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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;
}
 
Yes, Vlad, you are right. The code example in the book is not very good.
What you have written appeals to me much more.
Thanks for helping me.

Paul
Topic archived. No new replies allowed.