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;
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;
}
}
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
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."