Program To Print First n Primes Using A Function

I have an assignment to print the first n primes, where n is a number entered by the user. (It's a utility program so no prompts for entry are required). I'm making use of a function that tests for prime numbers. My issue is that I always return n+1 primes, and no matter how I change my while or for conditions this is still the case. What can I do to only print n primes?

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
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

bool isPrime(int n) {
	int count=0;
	if (n==1)
		return false;
	if (n==2)
		return true;
	if (n%2==0)
		return false;
	for (int i=1; i<=n; i++) {
		if (n%i==0)
			count++;
	}
	if (count==2)
		return true;
	else
		return false;
}

int main() {
	int n, count=0, possiblePrime=0;
	cin >> n;

	while(count<n) {
		if (n==1) {
			cout << 2;
			count=2;
		}
		for (int i=0; i<n; i++) {
			if	(isPrime(possiblePrime)) {
				cout << possiblePrime << " ";
				count++;
			}
		possiblePrime++;
		}
	}
			
	return 0;
}

You only need one loop (the one at line 26. Remove lines 31 & 37
Topic archived. No new replies allowed.