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

#include <iostream>
#include <sstream>
#include <string>
#define MAX 100000
using namespace std;

struct Queue {
  int head;
  int tail;
  int q[MAX];

  void initialize() {
    head = 0;
    tail = 0;
  }
  bool isEmpty() { return (head == tail); }
  bool isFull() { return (head == (tail + 1) % MAX); }
  void enqueue(int x) {
    if (isFull()) {
      cout << "queue overflow\\n";
      // exit(0);
    }
    q[tail] = x;
    if (tail == MAX - 1) {
      tail = 0;
    } else {
      tail += 1;
    }
  }
  int dequeue() {
    if (isEmpty()) {
      cout << "queue underflow\\n";
      // exit(0);
    }
    int x = q[head];
    if (head == MAX - 1) {
      head = 0;
    } else {
      head += 1;
    }
    return x;
  }
};

void calc(Queue q_name, string name[], Queue q_time, int quantum) {
  int t_sum = 0;
  while (q_name.head != q_name.tail) {
    if (q_time.q[q_time.head] <= quantum) {
      t_sum += q_time.dequeue();
      int i_name = q_name.dequeue();
      cout << name[i_name] << " " << t_sum << '\\n';
    } else {
      t_sum += quantum;
      q_name.enqueue(q_name.q[q_name.head]);
      q_time.enqueue(q_time.q[q_time.head] - quantum);

      if (q_name.head == MAX - 1) {
        q_name.head = 0;
        q_time.head = 0;
      } else {
        q_name.head += 1;
        q_time.head += 1;
      }
    }
  }
}

int main() {
  Queue q_name, q_time;
  int num, quantum;
  string name[MAX];
  int t;

  q_name.initialize();
  q_time.initialize();

  cin >> num >> quantum;
  for (int i = 0; i < num; i++) {
    string str;
    cin >> str;
    name[i] = str;
    cin >> t;
    q_name.enqueue(i);
    q_time.enqueue(t);
  }

  calc(q_name, name, q_time, quantum);

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
#define MAX 40000
using namespace std;

struct Queue {
  int head;
  int tail;
  int q[MAX];

  void initialize() {
    head = 0;
    tail = 0;
  }
  bool isEmpty() { return (head == tail); }
  bool isFull() { return (head == (tail + 1) % MAX); }
  void enqueue(int x) {
    if (isFull()) {
      cout << "queue overflow\\n";
      // exit(0);
    }
    q[tail] = x;
    if (tail == MAX - 1) {
      tail = 0;
    } else {
      tail += 1;
    }
  }
  int dequeue() {
    if (isEmpty()) {
      cout << "queue underflow\\n";
      // exit(0);
    }
    int x = q[head];
    if (head == MAX - 1) {
      head = 0;
    } else {
      head += 1;
    }
    return x;
  }
};

void calc(Queue q_name, char name[][10], Queue q_time, int quantum) {
  int t_sum = 0;
  while (q_name.head != q_name.tail) {
    if (q_time.q[q_time.head] <= quantum) {
      t_sum += q_time.dequeue();
      int i_name = q_name.dequeue();
      int j = 0;
      while (name[i_name][j] != '\\0') {
        cout << name[i_name][j];
        j += 1;
      }
      cout << " " << t_sum << '\\n';
    } else {
      t_sum += quantum;
      q_name.enqueue(q_name.q[q_name.head]);
      q_time.enqueue(q_time.q[q_time.head] - quantum);

      if (q_name.head == MAX - 1) {
        q_name.head = 0;
        q_time.head = 0;
      } else {
        q_name.head += 1;
        q_time.head += 1;
      }
    }
  }
}

int main() {
  Queue q_name, q_time;
  int num, quantum;
  char name[10][10];
  int t;

  q_name.initialize();
  q_time.initialize();

  cin >> num >> quantum;
  for (int i = 0; i < num; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < str.size(); j++) {
      name[i][j] = str[j];
    }
    cin >> t;
    q_name.enqueue(i);
    q_time.enqueue(t);
  }

  calc(q_name, name, q_time, quantum);

  return 0;
}