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 f = 1;
  for (int i = 1; i <= n; i++) {
    if (chair[i] == '#')
      continue;
    if (chair[i + 1] == '#')
      chair[i + 1] = 'C', i = n, f = 0;
    else if (chair[i - 1] == '#')
      chair[i - 1] = 'C', i = n, f = 0;
  }
  //  assert(f);
}

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;
}

Error: -6
bb7315f4-7f0c-408d-965b-db9cf6726c22.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00199/C++/s754021865.cpp:32: void C(int): Assertion `f' 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;
  // assert(i<=n);
}

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

void C(int kai) {
  if (kai == 0) {
    if (n % 2)
      chair[(n + 1) / 2] = 'C';
    else
      chair[n / 2 + 1] = 'C';
    return;
  }
  int f = 1;
  for (int i = 1; i <= n; i++) {
    if (chair[i] == '#')
      continue;
    if (chair[i - 1] == '#')
      chair[i - 1] = 'C', i = n, f = 0;
    if (chair[i + 1] == '#')
      chair[i + 1] = 'C', i = n, f = 0;
  }
  assert(f);
}

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();
    }
    assert(strlen(chair + 1) == n);
    printf("%s\\n", chair + 1);
  }
  return 0;
}