Calculating primes between 1 and 1000

Hello, I'm working on a class assignment. This function calculates numbers between 1 and 1000 for prime numbers. The error I'm having is variable count is not declared. Where do i declare the variable count within the nested for loop and will the prototype function primeCount, as written, return prime numbers?
// loop that calls primeCount (x,y) to test 1000 numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   long primeCount (long x, long y)
	{
	long i = 0;
	bool test;

	    for (i = x; i <= y; i++)
		{
			test = isPrime(i);
			if (test == true)
			{
			count++;
			}
			return (count);
			}
	}
The place where you declare count will determine what your function returns, but presumably you wrote the code, so why do you need someone else to tell you what its supposed to do?
I assume he just straight copy&pasted this from somewhere else, because even this was working it wouldn't "calculate" the prime numbers (I am not aware of a method to actually calculate a prime number...), but it would count the prime numbers in a given range.
It's amazing to me the value of education today; or may I say the level of courtesy one shares from their educational exploits.
I receive more value from Bench82's (20) reply than that of hanst99 (2256).
To answer your assumption hanst99, I copied and pasted primeCount from my .cpp file assignment from where I wrote it. My ignorant use of the word "calculate" when referring to the algorithm primeCount may have misled you.
For clarity, the assignment was to "Calculate and display the number of primes in the first 50 "chiliads"." Hence, my use of the word "calculate". We were also instructed to use two prototype functions outside main (primeCount) and (isPrime).
Not one to have someone do my homework for me, I only needed assistance with the primeCount function of the assignment; I believed. Wonderful thing being a student, we don't always know what we're talking about and consistently misuse terminology.
I appreciate all the assistance received.
-Todd
Obviously count should be defined and initialized to 0 at the start of the function.

In C++ you should prefer to define your loop counters in the for expressions (and in general define variables as close as possible to their first use.) You should not return count from inside the for loop, but after it. Returning from inside the for loop has the unfortunate side effect of returning from the function before the loop has finished running it's course.

for (long i = x; i <=y; i++)

You don't need the variable test. if( isPrime(i) )

Last edited on
Topic archived. No new replies allowed.