Bool Function for prime number program

I am trying to get a bool function to work in my program, but in the warnings it says that what i put will always result to being true, and i'm not sure why. I'm sure it is an obvious thing, but I am very new to c++ programming. Any help would be appreciated. Here's the code I have so far:

#include <iostream>
#include <iomanip>
using namespace std;
int prime = 0;
bool isPrime(int prime);

int main()
{
cout << "Please enter an integer and I will tell you if it is prime: " << endl;
cin >> prime;
if (isPrime)

cout << prime << " is a prime number. " << endl;

else
cout << prime << "is not a prime number. " << endl;
system ("pause");
return 0;
}
bool isPrime(int)
{

for ( int i = 0; i <= prime; i++)
{
if (( prime % i != 0) || ( i != 1) || (i != prime ))
{
return false;
}
else
return true;
}
}
I see you have problems with functions. A function knows nothing about the variables you use in main() or any other function. The idea is to pass the values you need in the parameter list: isPrime(prime). In the function definition you then manipulate those parameters.

Although choosing prime as a global is not a great idea (what if you want to test several numbers?), it can be made to work. The problem, I think, is that isPrime is written without ()s, so it is understood as the function itself, rather than the value it computes (a function pointer, you'll learn about those later). If you want to keep prime a global, another change to make is to remove int from declaration of isPrime, as you're not going to pass anything there and not going to use it.
Yes I do have lots of trouble with functions after trying to read up on it online and reading through a book I have about it. Would you be able to show me and example of a bool function that is correct? I'm still trying to mess around with mine and am still not getting close at all. This is where I am at so far, and I'm sure there's probably a logic error for finding out if the number is a prime number or not.


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


int main()
{
int prime = 0;
cout << "Please enter an integer and I will tell you if it is prime: " << endl;
cin >> prime;
bool isPrime(int prime);
if (isPrime)

cout << prime << " is a prime number. " << endl;

else
cout << prime << "is not a prime number. " << endl;
system ("pause");
return 0;
}


bool isPrime(int prime)
{

for ( int i = 0; i <= prime; i++)
{
if (( prime % i != 0) || ( i != 1) || (i != prime ))
{
return false;
}
else
return true;
}
}
Last edited on
Change bool isPrime to
1
2
3
4
5
6
bool isPrime(int prime){
for(int i=2;i<=prime/2;i++)
    if(!prime%i)//or if(prime%i==0)
       return 0;
return 1;
}
Last edited on
if(isPrime) should be if(isPrime(prime)).

Also, your logic is bad too.

Look at ( i != 1) || (i != prime ). This reads "i is not 1 or i is not 'prime'". Unless prime = 1, this is always true as no number can have two values. There two should be == instead.

Look at your if. It has return statements on both sides, meaning that the value of the function will be decided on the first iteration, when i = 0.

Also, your loop goes from 0 to prime. Firstly, there's a division by 0. Secondly, you know not to test 1 and prime itself, as prime will be divisible by these two no matter what it is, so why not make the loop from 1 to prime-1? Different if conditions could be used, but there is no point.

The code uhraurhua gave is almost correct. You should replace the if with the comment, or wrap prime%i in ()s. As an exercise, add explicit {}s for each control structure so that you understand what goes where.
Ok, thanks to your help, I got it to mostly work. The one thing I don't quite understand is the logic behind why this works to find a prime number in the function definition. Would you be able to walk it through with me please? I will show you what I put in the program, just let me know if I made any other errors with naming the functions and everything:

#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
char answer = 'y';
int prime;
bool isPrime(int prime);
while (answer == 'y' || answer == 'Y')
{
cout << "Please enter an integer and I will tell you if it is prime: " << endl;
cin >> prime;
if (prime <= 0)
{
cout << "Please make sure the number is above 0." << endl;
cin >> prime;
if (prime <=0)
{
cout << "The program will now terminate." << endl;
system("pause");
return 0;
}
}


if (isPrime(prime))

cout << prime << " is a prime number. " << endl;

else
cout << prime << " is not a prime number. " << endl;
system ("pause");
system("cls");
cout << "Would you like to try again? , enter (Y/y or N/n)" << endl;
cin >> answer;



}

return 0;
}


bool isPrime(int a)
{

for ( int i = 2; i <= a/2; i++)
{
if (a % i ==0)
return 0;
}

return true;

}




Like I said, it is working fine with figuring out the prime numbers and what aren't prime numbers, just not sure how it is doing the steps on the last part. Any insight would be appreciated, and I'm really thankful you have taken the time to help me out.
A prime number is a number that can only be divided by itself and 1. Since no number can be divided by a larger one, to know if A is prime, you only need to check all numbers smaller than A and larger than 1. The % operator returns the remainder of division and if B divides A, the remainder is 0. The A/2 constraint is a crude solution to the fact, that if B divides A, then so does (A/B), that is, divisors come in pairs and you really only need to check one half. Note that a better constraint would be i <= sqrt(a) or i*i <= a.
I know I am coming off as dumb, but I'm really trying to understand it by what you are saying and it's still not making sense. So take the function:

bool isPrime(int a)
{

for ( int i = 2; i <= a/2; i++)
{
if (a % i ==0)
return 0;
}

return true;

}
if int a was 7, then it would in all actuallity make it

bool isPrime(int a)
{

for ( int i = 2; i <= 3.5; i++)
{
if (a % i ==0)
return 0;
}

return true;

}
so it would loop through two times, from what I would understand, int i = 2, and int i = 3, which would be the last time that int i would be <= 3.5. Therefor it would be :
if (7 % 3 ==0)
return 0;

What I don't get is that you say divisors come in pairs. They do not need to equal the same thing though?"The A/2 constraint is a crude solution to the fact, that if B divides A, then so does (A/B), that is, divisors come in pairs and you really only need to check one half." My question is why does that matter when (A/B) = we will say : A =20 and B = 4. (20/4) = 5, but (4/20) = .2. Why only check one half if they come out to completely different things? I'm sorry, but I'm really trying to grasp the concept, and it is not coming to me at all.
The expression "A divides B" only really means anything if A and B are integers. That's sort of part of definition of what a prime number is.
When I said that divisors come in pairs, I meant that if A = 20 and B = 5, the pair of B is A/B = 4. All I mean is that if there is a large number dividing A, then there is also a small one. Knowing that, there is no point in searching for a divisor among large numbers. Here a large number is one > sqrt(A). You could notice that if you listed all divisors of a number.
Ok, I finally understand the logic behind what I did to find the prime number. Thanks a lot for the help hamsterman! Hopefully one day I can be as helpful as you were.
Topic archived. No new replies allowed.