My specifications are to prompt the user for an integer, then display a message indicating if the number is prime.
I know that a boolean value can only return as true or false. I also know that 0 means that the condition is false. How can I change my int function into a boolean function that the main function calls on, then states whether the number is prime or not?
Make function prime return bool, not int ;) Also change (in main) prm to bool. bools are an integral type (so int i = true; is valid), but it just makes more sense (imho) to make it return a bool.
Start your loop at 2 instead of 1 and end at sqrt(num) (ie see if numbers in the range 2 to sqrt(num) divide num).
The way you have it now, you check to see if divisor divides num, and if num does divide divisor, it returns flase, otherwise it returns true. Think about that for a second ;) You only want to return true after you have checked every number from 2 to sqrt(num) (meaning, after the loop has completed).
You also fail to inform the user of whether or not the number is prime. Is that by design or did you just omit it or something else?
If you want your function to return a boolean you have to change "int" prime to "????" prime. Also, if you want to print out true or false on your screen you'll have to change one of your variable's to bool(I know you'll figure out which one) and add something like this:
cout << boolalpha << variableyouareusing << endl;
Your if statement is not correct. It will always return 0/false, you have to work on it. One way to fix it is count how many times it was able to successfully get that % 0.
Now I have this, as I added suggestions from my teacher and from my main lessons, but my program is returning all sorts of error messages when I try to compile it and I'm really confused about how to make the boolean function return text in the main function.. Also I am unsure how to call the function in the main function correctly.
all sorts of error messages when I try to compile it
Copy + paste ;) It would help a bunches if you post the error messages.
1. You cannot define a function inside of another function,
2. The function prototype says the function is called isPrime, you make (what appears to be) another function prototype inside of main for a function isNumberPrime. Pick one name and leave the prototype before the main function,
3. By "a" do you mean "prime": for (int divisor = 2; divisor <=a/2; divisor++)? And I also said last time that you only need to check up to sqrt(prime),
4. You are missing a closing parenthesis for your if statement: if (isNumberPrime(prime) ) //<-- this one
I think that that is all that I had to fix to get it to run. Try it and see.
#include <cstdlib>
#include <iostream>
usingnamespace std;
bool isPrime(int prime);
int main(int argc, char *argv[])
{
int prime;
cout << "Enter an integer: ";
cin >> prime;
bool isPrime(prime);
if (isPrime(prime)) {
cout << prime << " is a prime number." << endl;
}
else {
cout << prime << " is not a prime number." << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
bool isPrime(int prime){
for (int divisor = 2; divisor <=prime/2; divisor++) {
if (prime%divisor==0)
return 0;
}
returntrue;
}
Here is what I have now and the error messages I receive are
" C:\Dev-Cpp\prime.cpp In function `int main(int, char**)':
18 C:\Dev-Cpp\prime.cpp `isPrime' cannot be used as a function"
Just a tip for the future: Your placement of the closing braces is a little confusing. For example on line 15, it looks as though it belongs to main, but it actually belongs to the if on line 13.
The answer is to put closing braces in the same column as the statement that needed an opening brace, like this :
#include <cstdlib> // is this needed?
#include <iostream>
usingnamespace std; // not such a good idea to do this, can cause naming conflicts
bool isPrime(int prime);
int main(int argc, char *argv[])
{
int prime;
std::cout << "Enter an integer: ";
std::cin >> prime;
if (isPrime(prime)) {
std::cout << prime << " is a prime number." << endl;
}
else {
std::cout << prime << " is not a prime number." << endl;
}
std::system("PAUSE");
return EXIT_SUCCESS;
} // end of main
bool isPrime(int prime) {
for (int divisor = 2; divisor <=prime/2; divisor++) {
if (prime%divisor==0) { // good practice to use braces even if there is 1 statement
returnfalse; // use true or false for bool values (just my preference)
}
}
returntrue; // confusion - is this in the right place? should it be inside the for loop?
}
Emma Naylor wrote:
It works!
Does it?
With prime numbers, one only has to check divisors as far as sqrt(TheNumber).
Your isPrime function in it's current form always returns true, no matter what the number is. If reorganised to have the returntrue; as the else statement, then it returns true for the first prime number, not the one you wish to test for.