Error in Prime number program

Him, im writing a program that will determine whether a number entered by a user is prime or not.
It tests the entered number by dividing all the numbers under its square root, and specifically excludes multiples of two.

The problem is that on line 42

---

sqNum=(sqrt(n));

---

It tells me this:42 [Warning] converting to `int' from `double'

Why??

______________________________


#include <iostream>
#include <cmath>
using namespace std;

int getInput ();
int detPrime (int n);
void printOutcome (int n, int result);

int main()

{
int n, result;
n=getInput();
result=detPrime(n);
printOutcome(n, result);

system ("PAUSE");
return 0;

}

int getInput()

{
int c=0;

while (c<=1)
{
cout<<"Please enter a number greater than one, for prime determination. "<<endl;
cin>>c;
}
return c;

}

int detPrime(int n)

{
int testNum, sqNum, divCounter, holderA, testerA, prime;
double holderB, testerB;

sqNum=(sqrt(n));

for(int j=1; j<=sqNum; j++)
{
for(j>1; testerA!=testerB; j++)
{
holderA=j/2;
holderB=j/2;

}

holderA=n/j;
holderB=n/j;

if (holderA==holderB)
{
divCounter++;
}

if(divCounter>2)
{
prime=0;
}
if(divCounter<=2)
{
prime=1;
}

return prime;
}

void printOutcome(int n, int result)
{
if(result==0)
cout<<"The number you have entered "<<n<<" is Not Prime."<endl;
}
if(result==1)
{cout<<"The number you have entered "<<n<<" IS Prime."<<endl;
}
}

___________________________

Thanks for the help in advance.
sqrt() returns a double.

sqNum is an int.

Therefore you are asking the compiler to convert a floating point number to an integer.

The compiler won't do this automatically as a safeguard, because it doesn't know if you
really mean it, or just made a mistake. You have to tell the compiler you meant it by explicitly casting the return value of sqrt() to int.
Okay, i posted prematurely, now the program does compile, but it doesnt really work as intended, can anyone see why? heres the version that actually does compile

Edit: Updated code

_____________________________________________________

#include <iostream>
#include <cmath>
using namespace std;

int getInput ();
int detPrime (int n);
void printOutcome (int n, int result);

int main()

{
int n, result;
n=getInput();
result=detPrime(n);
printOutcome(n, result);

system ("PAUSE");
return 0;

}

int getInput()

{
int c=0;

while (c<=1)
{
cout<<"Please enter a number greater than one, for prime determination. "<<endl;
cin>>c;
}
return c;

}

int detPrime(int n)

{
int testNum, divCounter, sqNum, holderA, testerA, prime;
double holderB, testerB;

sqNum=(sqrt(n));

for(int j=1; j<=sqNum; j++)
{
for(j>1; testerA!=testerB; j++)
{
holderA=j/2;
holderB=j/2;

}

holderA=n/j;
holderB=n/j;

if (holderA==holderB)
{
divCounter++;
}

if(divCounter>2)
{
prime=0;
}
if(divCounter<=2)
{
prime=1;
}
}
return prime;

}

void printOutcome(int n, int result)
{
if(result==0)
{
cout<<"The number you have entered "<<n<<" is Not Prime."<<endl;
}
if(result==1)
{
cout<<"The number you have entered "<<n<<" IS Prime."<<endl;
}
if(result>1)
{cout<<result;}
}

Also, jsmith, how do i do that?

"You have to tell the compiler you meant it by explicitly casting the return value of sqrt() to int."
Last edited on
If you really want sqNum to be an integer, and don't care about losing precision, you could typecast it like this so you don't get the warning:

sqNum = static_type<int>(sqrt(n));
Last edited on
Topic archived. No new replies allowed.