I'm working on an exercise for pointers. I'm supposed to calculate the primes from <= x. Inside of my FOR loop I get the "expression must have integral or enum type" error. I think it has to do with i being an int and x being a double. I've tried a lot of things and nothing seems to make it work.

Any suggestions?

#include <iostream>

int pi(double x)

{

int * numprimes;

numprimes = new int;

// Declare pointers and allocate memory for all of them

// Compute number of primes <= x and store at integer that

// numprimes points to.

int count = 0;

for (int i = 2; i <= x; i++)

{

if (x % i != 0)

{

count++;

}

}

*numprimes = count;

int ret = *numprimes;

// Deallocate memory for all pointers declared

delete numprimes;

return ret;

}

int main()

{

double x;

std::cin >> x;

std::cout << pi(x) << '\n';

}

Any suggestions?

#include <iostream>

int pi(double x)

{

int * numprimes;

numprimes = new int;

// Declare pointers and allocate memory for all of them

// Compute number of primes <= x and store at integer that

// numprimes points to.

int count = 0;

for (int i = 2; i <= x; i++)

{

if (x % i != 0)

{

count++;

}

}

*numprimes = count;

int ret = *numprimes;

// Deallocate memory for all pointers declared

delete numprimes;

return ret;

}

int main()

{

double x;

std::cin >> x;

std::cout << pi(x) << '\n';

}

Last edited on

Why are you declaring numprimes as a pointer? That is unnecessarily difficult. That's not the way to learn about pointers.

Why does PI take a double as an input? By definition, a prime number is a natural number.

http://math.about.com/od/mathhelpandtutorials/a/Understanding-Classification-Of-Numbers.htm

http://en.wikipedia.org/wiki/Prime_number

Why does PI take a double as an input? By definition, a prime number is a natural number.

http://math.about.com/od/mathhelpandtutorials/a/Understanding-Classification-Of-Numbers.htm

http://en.wikipedia.org/wiki/Prime_number

If you want to try excersizes on pointers try solving simple problems like swapping array elements etc. i don't understand as to why you need the pointer in the above program it is completely unnecessary. Also primes are natural numbers not fractions.

Type double has no operator **%**. So this statement

f (x % i != 0)

is invalid because x has type double and you are trying to apply the operator %.

I think that you should consider integer numbers that less than x in the loop.

f (x % i != 0)

is invalid because x has type double and you are trying to apply the operator %.

I think that you should consider integer numbers that less than x in the loop.

Last edited on

What every one else said, plus this:

And one should never use doubles in a for loop, because the equality operator will fail, causing it to go right past the end condition.

Could you also remember to always use code tags - the <> button on the right.

And one should never use doubles in a for loop, because the equality operator will fail, causing it to go right past the end condition.

Could you also remember to always use code tags - the <> button on the right.

Topic archived. No new replies allowed.