Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <fstream>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <algorithm>
#include <cmath>
#include <functional>
#include <numeric>

using namespace std;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{ ";
  for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
    os << '\\"' << *it << '\\"' << (it + 1 == v.end() ? "" : ", ");
  os << " }";
  return os;
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define vv(type, w, h, init) vector<vector<type>>(h, vector<type>(w, init))
typedef vector<string> vs;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long ul;
typedef unsigned long long ull;

#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define mp make_pair

string const resstr[] = {"Impossible", "Possible", "No", "Yes"};

bool p[1000100];

void erato() {
  fill(p, p + 1000100, true);
  p[0] = p[1] = false;

  for (int i = 0; i < 1000100; i++) {
    if (!p[i])
      continue;
    for (int j = i + i; j < 1000100; j += i) {
      p[j] = false;
    }
  }
}

int main() {
  int n;
  erato();
  while (cin >> n) {
    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i])
        count++;
    }
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

#include <fstream>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <algorithm>
#include <cmath>
#include <functional>
#include <numeric>

using namespace std;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{ ";
  for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
    os << '\\"' << *it << '\\"' << (it + 1 == v.end() ? "" : ", ");
  os << " }";
  return os;
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define vv(type, w, h, init) vector<vector<type>>(h, vector<type>(w, init))
typedef vector<string> vs;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long ul;
typedef unsigned long long ull;

#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define mp make_pair

string const resstr[] = {"Impossible", "Possible", "No", "Yes"};

bool p[100010];

void erato() {
  fill(p, p + 100010, true);
  p[0] = p[1] = false;

  for (int i = 0; i < 100010; i++) {
    if (!p[i])
      continue;
    for (int j = i + i; j < 100010; j += i) {
      p[j] = false;
    }
  }
}

int main() {
  int n;
  erato();
  while (cin >> n) {
    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i])
        count++;
    }
    cout << count << endl;
  }
}