Help Counting Printed Numbers

Hey everyone, I need some help with this homework assignment. I have to write a program that prints all of the prime numbers from 2 to 100,000 and prints the number of primes between 2 and 100,000. I got my code to print all of the primes but I have no idea how to count the number of primes that were printed. Is there some sort of counter that I need?

Here is the code that prints the prime numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Lists prime numbers under 100,000: 
  int p;                                                                
  for (int i=2; i<100000; i++)
    {
      if (p<i)
      {
        p=i;
      }
      bool prime=true;
      for (int x=2; x*x<=i; x++)
        {
          if (i % x == 0)
            {
              prime=false;
              break;
            }
        }
      if(prime) cout << i << " ";
    }
  cout << p << endl;

Thanks in advance for any help!
Last edited on
int totalPrimes=0;

When you determine a number is prime

totalPrimes++;

After the loop ends

cout << totalPrimes;
Topic archived. No new replies allowed.