Prime or not?

So I've finished a code but when I submitted it to the grading program, it says this:
cc1plus: warnings being treated as errors
Primenum.cpp: In function 'bool isPrime(int)':
Primenum.cpp:17: error: control reaches end of non-void function
compilation terminated due to -Wfatal-errors.

I've had this before and basically it's saying that my code eventually runs to a point where my isPrime(int) function doesn't return anything. I've solved this issue in the past but it was the beginning of the semester and I don't remember what I had to do. Here is my code:

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
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

bool isPrime(int a)
{
	int prime;
	for( int i = 2; i < a; i++ )
	{
		prime = a % i;
		if( prime == 0)
		{
			return false;
		}
	}

	if(prime != 0)
		return true;
}

int main()
{
	float number;
	cout << "Enter number: ";
	cin >> number;

	cout << endl;

	if( isPrime(number) == true)
	{
		cout << number << " is prime.";
	}
	else
	{
		cout << number << " is not prime.";
	}
}


I get all the correct answers unless I type in 2 or lower, then the answer is wrong. I'm still working on that as well so if someone knows what's going on there that would be good to know. I don't know why 2 is considered prime in my code because if 2 was entered: 2 % 2 = 0 so when 0 is put in place for "prime", it is supposed to return false which means it's not prime.
To explain the compiler error:
Suppose you reach line 16 and prime is 0. What does the function return then?

Why 2 and below are prime by your code:
a is <2, so and i=2 is not less than a<=2, so the loop is not entered. prime, being uninitialized, is likely to not be zero, so the function returns true.
At line 16, if prime is 0, then it would have already returned false from the for loop would it not?

I simply inserted this:
1
2
3
4
if(a == 2)
	{
		return false;
	}

To return "not prime" if 2 was entered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool isPrime(int a)
{
        if (~a & 1 && a != 2)
               return false;

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

	return true;
}
Last edited on
I've never seen this before:
1
2
   if (~a & 1) // if a is even...
            return true;


What does that mean?
Keep in mind I'm very new to programming.
It is equivalent to (a % 2) == 0. It is the bitwise version of that
Ok. I see. So really I needed the "return true" at the very end of the function.
My compile program is now saying that 2 has to be prime so I can just forget about the part that makes 2 not prime...even though 2 is not prime.
Er, 2 is a prime number. Could you try explaining the issue a different way, maybe...?
Oh wow it is a prime number...I'm an idiot. I was thinking too much into modular division...

The answer to my problem was that I had no return value if the other conditions weren't met like you said.

Thanks for the help everyone.
Yeap, 2 is a prime number. And in your loop you could test just until i <=sqrt(a).
If there is no i < sqrt(a) for which ( a%i )== 0 then there won't be an i between sqrt(a)and a for which ( a%i )== 0 as well.
Topic archived. No new replies allowed.