There are various problems inside your code:
The is_prime()
function should be like:
bool is_prime(int n) // <-- No use of 'is_prime' parameter here...
{
bool is_prime = true; // should be outside the loop...
for (int i = 2; i < n; i++)
{
if (n % i == 0)
is_prime = false;
}
return is_prime;
}
and the sumPrime()
function:
int sumPrime(int x1, int x2)
{
/*int x1, y1; Just forget these variables, they are not even initialized*/
int sum = 0;
for ( /*x1 <- It is not syntatically correct, it is not an expression, so just leave it blank*/; x1 < x2; x1++)
{
if (is_prime(x1))
{
sum += x1;
}
}
return sum;
}
Explanation:
The is_prime()
function..., what you have done here is that you have declared the variable is_prime
(Not the function, look closely) both in the parameters and also inside the loop...
This, in fact, will not cause a problem, but will shadow your previous declaration...
Also, there is no need for is_prime
to be present in the parameters because it is mostly useless (Maybe because I don't know what you are trying to achieve)... But you have to choose one, so there is something like this you can do:
bool is_prime(int n, bool& is_prime) // Make is_prime a reference and assign to it...
Also, change this line:
bool is_prime = true;
to:
is_prime = true; // Remove bool specifier, don't declare 'is_prime' again!
As for your other function, it is, in fact, not even an old syntax, and don't even ask about C++, the only way you can declare functions in C++ is:
<return_type> <function-name>(<parameters>) { <body> }
Note that this is pseudo-syntax of a function declaration and is followed by most languages nowadays...
So your function should also look like:
bool is_prime(int x1, int x2) { /* <body> */ }
And also remove the declarations of x1 and x2 inside the function to prevent variable shadowing (Just like the above example)...
Edit: Also, Looking at these small mistakes, anyone will tell you to look at a good C++ book...