Prime Number Finder

So I'm working on Project Euler question 7, find the 10 001st prime. I've tried to work out why my code is failing for a while now and I still can't get it. I thought that if I built an array of the primes I could just check any new number against all the elements in the array, and if it didn't divide nicely with any of them then that number would be prime.

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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a,b(1),d;
    double n(2);
    cin >> a;
    double primes[a];
    primes[0]=2.0;
    for (d=0;d<=a-1;d++)
    {
        primes[d]=1.0;
    }

    while (b<=a)
    {
        int c(0);
        while (c<=(b-1)&&primes[b]!=0)
        {
            if (fmod(n,primes[c])!=0)
            {
                primes[b]=n;
                cout << primes[b];
            }
            else primes[b]=0
            c++;
        }
        primes[b]!=0?b++:n++;
    }
return 0;

}
my code is failing for a while now
Please describe more precisely.

I think it wouldn't compile? So have a look at line 11. In C++ the size of an array has to be known at compile time.
Topic archived. No new replies allowed.