post  yet another prime number problem!!!

butterflyze (7)   Link to this post
Hi!
I am new to this website and Ive seen many prime number questions asked but none about my specific problem. It may sound simple but Ive tried everything and just can not get past two syntax errors! Please and Thank You for helping!!!

Here is my code:

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


bool prime(double num, int j);



int main()
{
double num = 10000;

for (int j = 3; j <= 10000; j++)
{
bool prime(double num, int j);

if (bool prime (double num, int j)) // error one is here (?)
{ // error two is actually on this line (yes
cout << " " << j << " "; // the bracket) (??)
}


}


return 0;
}



bool prime(double num, int j)
{
bool prime = true;
for(int i = 2; i <= sqrt(num); i++)
{
if(i % 2 == 0)
i++;

if((int(num)% i) == 0)
{
prime = false;
break;
}
}

return true;
}





Im using Visual C++ Express Edition 2005 and the errors it gives me at the bottom are:

error 1:
1>c:\users\jennifer\desktop\midterm_3\morrisjchapter6_30_revised.cpp(18) : error C2059: syntax error : ')'

error 2:
1>c:\users\jennifer\desktop\midterm_3\morrisjchapter6_30_revised.cpp(19) : error C2143: syntax error : missing ';' before '{'
helios (4790)   Link to this post
I don't see any questions.

When you call a function, you only pass the arguments. You don't need to also provide the types of the arguments, nor the function's return type:
if (prime(num,j))

EDIT: Sometimes you do need to specify what type an argument is: when the function is overloaded and an argument could be interpreted as one of several types.
Last edited on
butterflyze (7)   Link to this post
thank you helios. that solved the errors.
butterflyze (7)   Link to this post
k now that the errors are gone, why is my program printing out all the numbers instead of just the primes?
helios (4790)   Link to this post
prime() unconditionally returns true.
butterflyze (7)   Link to this post
ive been looking at that too.....ill try and experiment a little further with it before i ask for more help. thanks!
chrisname (1342)   Link to this post
You're not returning false at any point. What he means by it "unconditionally returns true" is that you've hard-coded "return true". So no matter what happens; if pigs fly, it won't return false.

I think on the return statement you want to return "prime".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool prime(double num, int j)
{
    bool prime = true;
    for(int i = 2; i <= sqrt(num); i++)
    {
        if(i % 2 == 0)
            i++;

        if((int(num % i)) == 0)
        {
            prime = false;
            break;
        }
    }

    return prime;
}
butterflyze (7)   Link to this post
thanks for that chrisname this is what i ended up doing for my function:




bool prime(double num, int j)
{
bool prime = false;

for(int i = 2; i <= sqrt(num); i++)
{

if(j != i && j % i == 0)
{
return prime;
break;
}

}
return prime = true;
}





and my program finally works now!

Registered users can post in this forum.