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

#include <bits/stdc++.h>
using namespace std;
int n, m;
char chair[200];

void A(char ch) {
  int i = 1;
  while (chair[i] != '#')
    i++;
  chair[i] = ch;
}

void B() {
  int i = n;
  while (chair[i] != '#')
    i--;
  chair[i] = 'B';
}

void C(int kai) {
  if (kai == 0) {
    if (n % 2)
      chair[(n + 1) / 2] = 'C';
    else
      chair[n / 2 + 1] = 'C';
    return;
  }

  int i = 0;
  for (i = 1; i <= n; i++) {
    if (chair[i] == '#')
      continue;
    if (chair[i + 1] == '#')
      chair[i + 1] = 'C', i = n + 2;
    else if (chair[i - 1] == '#')
      chair[i - 1] = 'C', i = n + 2;
  }
  assert(i != n + 2);
}

void D() {
  int R = 0, L = 0;
  while (chair[n - R] == '#')
    R++;
  while (chair[L + 1] == '#')
    L++;

  int M = 0, idx = 0, cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (chair[i] == '#')
      cnt++;
    else
      cnt = 0;
    if (M + M % 2 < cnt)
      M = cnt, idx = i;
  }
  if (M <= 2 && L <= 1 && R <= 1)
    A('D');
  else if (L - 1 >= (M - 1) / 2 && L - 1 >= R - 1)
    chair[1] = 'D';
  else if (R - 1 > (M - 1) / 2)
    chair[n] = 'D';
  else
    chair[(idx - M + 1) + (M - 1) / 2] = 'D';
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    memset(chair, '#', sizeof(chair));
    chair[0] = chair[n + 1] = 0;
    for (int i = 0; i < m; i++) {
      char a;
      cin >> a;
      if (a == 'A')
        A(a);
      if (a == 'B')
        B();
      if (a == 'C')
        C(i);
      if (a == 'D')
        D();
    }
    printf("%s\\n", chair + 1);
  }
  return 0;
}

Error: -6
55ee778c-e3a1-469d-a1d8-9a312d148150.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00199/C++/s518549103.cpp:32: void C(int): Assertion `i!=n+2' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m;
char chair[200];

void A(char ch) {
  int i = 1;
  while (chair[i] != '#')
    i++;
  chair[i] = ch;
}

void B() {
  int i = n;
  while (chair[i] != '#')
    i--;
  chair[i] = 'B';
}

void C(int kai) {
  if (kai == 0) {
    if (n % 2)
      chair[(n + 1) / 2] = 'C';
    else
      chair[n / 2 + 1] = 'C';
    return;
  }

  int i = 0;
  for (i = 1; i <= n; i++) {
    if (chair[i] == '#')
      continue;
    if (chair[i + 1] == '#')
      chair[i + 1] = 'C', i = n + 1;
    else if (chair[i - 1] == '#')
      chair[i - 1] = 'C', i = n + 1;
  }
  assert(i != n + 2);
}

void D() {
  int R = 0, L = 0;
  while (chair[n - R] == '#')
    R++;
  while (chair[L + 1] == '#')
    L++;

  int M = 0, idx = 0, cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (chair[i] == '#')
      cnt++;
    else
      cnt = 0;
    if (M + M % 2 < cnt)
      M = cnt, idx = i;
  }
  if (M <= 2 && L <= 1 && R <= 1)
    A('D');
  else if (L - 1 >= (M - 1) / 2 && L - 1 >= R - 1)
    chair[1] = 'D';
  else if (R - 1 > (M - 1) / 2)
    chair[n] = 'D';
  else
    chair[(idx - M + 1) + (M - 1) / 2] = 'D';
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    memset(chair, '#', sizeof(chair));
    chair[0] = chair[n + 1] = 0;
    for (int i = 0; i < m; i++) {
      char a;
      cin >> a;
      if (a == 'A')
        A(a);
      if (a == 'B')
        B();
      if (a == 'C')
        C(i);
      if (a == 'D')
        D();
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (chair[i] != '#')
        cnt++;
    assert(cnt == m);
    printf("%s\\n", chair + 1);
  }
  return 0;
}