alexjercan/bugnet huggingface dataset:
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <ctype.h>
#include <iostream>
#include <string>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  string str;
  int pre, pos;
  stack<int> s;
  getline(cin, str);
  istringstream is(str);
  while (is >> str) {
    if (atoi(str.c_str()) != 0) {
      s.push(atoi(str.c_str()));
    } else {
      if (str == "0") {
        s.push(atoi(str.c_str()));
        continue;
      }
      pos = s.top();
      s.pop();
      pre = s.top();
      s.pop();
      if (str == "+")
        s.push(pre + pos);
      if (str == "-")
        s.push(pre - pos);
      if (str == "*")
        s.push(pre * pos);
    }
  }

  cout << s.top() << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <ctype.h>
#include <iostream>
#include <string>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  string str;
  int pre, pos;
  stack<int> s;
  getline(cin, str);
  istringstream is(str);
  while (is >> str) {
    if (atoi(str.c_str()) != 0)
      s.push(atoi(str.c_str()));
    else {
      pos = s.top();
      s.pop();
      pre = s.top();
      s.pop();
      if (str == "+")
        s.push(pre + pos);
      if (str == "-")
        s.push(pre - pos);
      if (str == "*")
        s.push(pre * pos);
    }
  }

  cout << s.top() << endl;

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100

int main() {

  int x, top = 0;
  char s[N];
  int num[N];

  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      num[top - 2] = num[top - 2] + num[top - 1];
      top--;
    } else if (s[0] == '-') {
      num[top - 2] = num[top - 2] - num[top - 1];
      top--;
    } else if (s[0] == '*') {
      num[top - 2] = num[top - 2] * num[top - 1];
      top--;
    } else {
      num[top] = atoi(s);
      top++;
    }
  }
  printf("%d\\n", num[0]);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#define N 100

int s[3];
int top;

void initialize(void);
int isFull(void);
int isEmpty(void);
void push(int);
int pop();

int main() {

  char sss[N];
  int num1, num2, x;

  initialize();

  while (scanf("%s", sss) != EOF) {

    if (sss[0] == '+') {
      num1 = pop();
      num2 = pop();
      push(num2 + num1);
    }

    else if (sss[0] == '-') {
      num1 = pop();
      num2 = pop();
      push(num2 - num1);
    }

    else if (sss[0] == '*') {
      num1 = pop();
      num2 = pop();
      push(num2 * num1);
    }

    else {
      x = atoi(sss);
      push(x);
    }
  }

  printf("%d\\n", pop());

  return 0;
}

void initialize(void) { top = 0; }

int isEmpty(void) {
  if (top == 0)
    return 1;
  return 0;
}

int isFull(void) {
  if (top >= N - 1)
    return 1;
  return 0;
}

void push(int x) {
  if (isFull()) {
    exit(1);
  }
  top++;
  s[top] = x;
}

int pop(void) {
  if (isEmpty()) {
    exit(1);
  }
  top--;
  return s[top + 1];
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  stack<int> s;
  string str;
  while (cin >> str) {
    if (str == "+") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(a + b);
    } else if (str == "-") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(-a + b);
    } else if (str == "*") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(a * b);
    } else {
      int a = stoi(str);
      s.push(a);
    }
  }
  cout << s.top() << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  stack<int> s;
  string str;
  while (true) {
    cin >> str;
    if (str == "end" || str == "\\n") {
      cout << s.top() << endl;
      break;
    }
    if (str == "+") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(a + b);
    } else if (str == "-") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(-a + b);
    } else if (str == "*") {
      int a = s.top();
      s.pop();
      int b = s.top();
      s.pop();
      s.push(a * b);
    } else {
      int a = stoi(str);
      s.push(a);
    }
  }
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

const int MAX = 100;
int top, s[MAX + 1];

void initialize() { top = 0; }

bool isEmpty() { return top == 0; }

bool isFull() { return top >= MAX; }

void push(int x) {
  if (isFull()) {
    fprintf(stderr, "error: stack is full\\n");
    exit(1);
  }
  top++;
  s[top] = x;
}

int pop() {
  if (isEmpty()) {
    fprintf(stderr, "error: stack is empty\\n");
    exit(1);
  }
  top--;
  return s[top + 1];
}

int main() {
  initialize();
  string str;
  while (cin >> str) {
    if (isdigit(str[0])) {
      push(atoi(str.c_str()));
    } else {
      int b = pop();
      int a = pop();
      if (str == "+")
        push(a + b);
      else if (str == "-")
        push(a - b);
      else if (str == "*")
        push(a * b);
    }
  }
  printf("%d\\n", pop());
  return 0;
}

Error: 1
error: stack is empty


Fixed submission:

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const int MAX = 100;
int top, s[MAX + 1];

void initialize() { top = 0; }

bool isEmpty() { return top == 0; }

bool isFull() { return top >= MAX; }

void push(int x) {
  if (isFull()) {
    fprintf(stderr, "error: stack is full\\n");
    exit(1);
  }
  top++;
  s[top] = x;
}

int pop() {
  if (isEmpty()) {
    fprintf(stderr, "error: stack is empty\\n");
    exit(1);
  }
  top--;
  return s[top + 1];
}

int main() {
  initialize();
  char str[10];
  while (cin >> str) {
    if (isdigit(str[0])) {
      push(atoi(str));
    } else {
      int a = pop();
      int b = pop();
      if (str == "+")
        push(a + b);
      else if (str == "-")
        push(a - b);
      else if (str == "*")
        push(a * b);
    }
  }
  printf("%d", pop());
  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[105];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

// int ToInt(char *s){
//	int res=s[0]-'0';
//	for(int i=1;s[i]!=0;i++){
//		res = res*10+s[i]-'0';
//	}
//
//	return res;
// }

int main() {
  int a, b;
  top = 0;
  char s[105];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\\n", pop());

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[100];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

// int ToInt(char *s){
//	int res=s[0]-'0';
//	for(int i=1;s[i]!=0;i++){
//		res = res*10+s[i]-'0';
//	}
//
//	return res;
// }

int main() {
  int a, b;
  top = 0;
  char s[105];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\\n", pop());

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) Rep(i, 0, n)
#define Rep(i, k, n) for (int i = k; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define vi vector<int>
#define vii vector<int, int>
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())

// const int MOD = 1000000007;
// const int INF = 1<<30;
using namespace std;

string yn(bool x) { return x ? "Yes" : "No"; }

int calc(int x, int y, string s) {
  if (s == "+")
    return x + y;
  if (s == "-")
    return x - y;
  if (s == "*")
    return x * y;
  return 0;
}

int main() {
  stack<int> a;
  string s;
  while (cin >> s) {
    // if(cin.eof()) break;

    if (s == "+" || s == "-" || s == "*") {
      int numA = a.top();
      a.pop();
      int numB = a.top();
      a.pop();

      a.push(calc(numB, numA, s));
    } else {
      int x = stoi(s);
      a.push(x);
    }

    if (getchar() == '\\n')
      break;
  }

  cout << a.top() << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) Rep(i, 0, n)
#define Rep(i, k, n) for (int i = k; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define vi vector<int>
#define vii vector<int, int>
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())

// const int MOD = 1000000007;
// const int INF = 1<<30;
using namespace std;

string yn(bool x) { return x ? "Yes" : "No"; }

int calc(int x, int y, string s) {
  if (s == "+")
    return x + y;
  if (s == "-")
    return x - y;
  if (s == "*")
    return x * y;
  return 0;
}

int main() {
  stack<int> a;
  string s;
  while (cin >> s) {
    // if(cin.eof()) break;

    if (s >= "0" and s <= "9") {
      int x = stoi(s);
      a.push(x);
    } else {
      int numA = a.top();
      a.pop();
      int numB = a.top();
      a.pop();

      a.push(calc(numB, numA, s));
    }

    if (getchar() == '\\n')
      break;
  }

  cout << a.top() << endl;
}
Problem p02263 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

int main() {
  char c[100], s[200];
  stack<int> a;
  int j = 0, k = 0;
  while (scanf("%s", s) == 1) {
    int b, c;
    if (strcmp(s, "+") == 0) {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(b + c);
    } else if (strcmp(s, "-") == 0) {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(c - b);
    } else if (strcmp(s, "*") == 0) {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(b * c);
    } else {
      a.push(atoi(s));
    }
    if (strchr(s, '\\n') != NULL) {
      break;
    }
    j++;
  }
  /*
          REP(i, k){
                  int b, c;
                  if (s[i] == '+'){
                          b = a.top();
                          a.pop();
                          c = a.top();
                          a.pop();
                          a.push(b + c);
                  }
                  else if (s[i] == '-'){
                          b = a.top();
                          a.pop();
                          c = a.top();
                          a.pop();
                          a.push(c - b);
                  }
                  else if (s[i] == '*'){
                          b = a.top();
                          a.pop();
                          c = a.top();
                          a.pop();
                          a.push(b * c);
                  }
                  else{
                          a.push(s[i]-'0');
                  }
                  printf("%d\\n", a.top());
          }
  */
  printf("%d\\n", a.top());

  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

int main() {
  char s[200];
  stack<int> a;
  int j = 0;
  while (scanf(" %c", &s[j]) != '\\n') {
    j++;
  }
  REP(i, strlen(s) - 1) {
    int b, c;
    if (s[i] == '+') {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(b + c);
    } else if (s[i] == '-') {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(b - c);
    } else if (s[i] == '*') {
      b = a.top();
      a.pop();
      c = a.top();
      a.pop();
      a.push(b * c);
    } else {
      a.push((int)s[i]);
    }
  }

  printf("%d\\n", a.top());

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
// #define MAX_SYMBOL 300//シンボルの最大数
using namespace std;

class SimpleStack {
private:
  vector<int> v;

public:
  void put(int n) { v.push_back(n); }

  int pop() {
    int val;
    if (!v.empty()) {
      val = *(v.end() - 1);
      v.pop_back();
      return val;
    }
  }
};

//&を使って参照渡しにすることでコピーによるメモリ消費を回避できる
// しかも扱いは->でなく.でOK
// http://homepage2.nifty.com/well/Reference.html#PASS_BY_REFERENCE の3
double calc(char q, SimpleStack &ss) {
  double a;
  double b;
  if (q == '+') {
    a = ss.pop();
    b = ss.pop();
    ss.put(a + b);
    return a + b;
  } else if (q == '-') {
    a = ss.pop();
    b = ss.pop();
    ss.put(b - a);
    return b - a; // a-bでないことに注意！
  } else if (q == '*') {
    a = ss.pop();
    b = ss.pop();
    ss.put(a * b);
    return a * b;
  }
  return 0;
}

int main() {
  SimpleStack ss;
  char s[100]; // 1単位
  int num;
  int i = 0;

  while (scanf("%s", s) != EOF) {

    if (s[0] == '+' || s[0] == '-' || s[0] == '*')
      calc(s[0], ss);
    else {
      num = atoi(s);
      ss.put(num);
    }
  }
  cout << ss.pop() << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;

class SimpleStack {
private:
  vector<int> v;
  vector<int>::iterator itr_end;

public:
  void put(int n) { v.push_back(n); }

  int pop() {
    int val;
    if (!v.empty()) {
      itr_end = v.end(); // back()はイテレータではなく参照を返すことに注意
      itr_end--; // end()は最終要素の次の要素のイテレータを返す
      val = *itr_end;
      v.pop_back();
      return val;
    }
  }

  bool isEnd() {
    if (v.end() - 1 == v.begin())
      return true;
    return false;
  }
};

double calc(char q, SimpleStack *ss) {
  double a;
  double b;
  if (q == '+') {
    a = ss->pop();
    b = ss->pop();
    ss->put(a + b);
    return a + b;
  } else if (q == '-') {
    a = ss->pop();
    b = ss->pop();
    ss->put(b - a);
    return b - a; // a-bでないことに注意！
  } else if (q == '*') {
    a = ss->pop();
    b = ss->pop();
    ss->put(a * b);
    return a * b;
  }
  return 0;
}

int main() {
  SimpleStack *ss;
  ss = new SimpleStack;
  char Cdata;
  int Idata;

  scanf("%s", &Cdata);
  Idata = atoi(&Cdata);
  ss->put(Idata);
  while (scanf("%s", Cdata) != EOF) { // この書き方！
    if (Cdata == '+' || Cdata == '-' || Cdata == '*') {
      calc(Cdata, ss);
    } else {
      Idata = atoi(&Cdata);
      ss->put(Idata);
    }
  }
  cout << ss->pop() << endl;

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string word;
  stack<int> S;
  while (cin >> word) {
    int a, b;
    if (word == "+") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(a + b);
      // cout << "+" << a+b;
    } else if (word == "-") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(b - a);
      // cout << "-" << b-a;
    } else if (word == "*") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(a * b);
      // cout << "*" << a*b;
    } else {
      S.push(atoi(word.c_str()));
    }
    // cout << S.top() << endl;
  }
  cout << S.top() << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string word;
  stack<int> S;
  while (cin >> word) {
    int a, b;
    if (word == "+") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(a + b);
    }
    if (word == "-") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(a - b);
    }
    if (word == "*") {
      a = S.top();
      S.pop();
      b = S.top();
      S.pop();
      S.push(a * b);
    } else {
      S.push(stoi(word));
    }
  }
  cout << S.top() << endl;
  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

class Stack {
private:
  int container[100];
  int num;

public:
  Stack() { num = 0; };
  void show() {
    for (int i = 0; i < num; i++) {
      std::cout << container[i] << ' ';
    }
    std::cout << '\\n';
  };
  void push(int val) { container[num++] = val; };
  int pop() {
    num--;
    return container[num];
  };
};

int main(void) {
  std::string s;
  int a, b;
  Stack stack = Stack();

  while (std::cin >> s) {
    if (s == "+") {
      b = stack.pop();
      a = stack.pop();
      stack.push(a + b);
    } else if (s == "-") {
      b = stack.pop();
      a = stack.pop();
      stack.push(a - b);
    } else if (s == "*") {
      b = stack.pop();
      a = stack.pop();
      stack.push(a * b);
    } else {
      int val = std::stoi(s);
      stack.push(val);
    }
    //    stack.show();
  }

  std::cout << stack.pop() << '\\n';

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

class Stack {
private:
  int container[100];
  int num;

public:
  Stack() { num = 0; };
  void push(int val) { container[num++] = val; };
  int pop() {
    num--;
    return container[num + 1];
  };
};

int main(void) {
  std::string s;
  char in[100];
  int a, b;
  Stack stack = Stack();

  while (std::cin >> s) {
    if (s[0] == '+') {
      b = stack.pop();
      a = stack.pop();
      stack.push(a + b);
    } else if (s[0] == '-') {
      b = stack.pop();
      a = stack.pop();
      stack.push(a - b);
    } else if (s[0] == '*') {
      b = stack.pop();
      a = stack.pop();
      stack.push(a * b);
    } else {
      int val = std::stoi(in);
      stack.push(val);
    }
  }

  std::cout << stack.pop() << '\\n';

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string s;
  stack<int> St;
  int l, r;
  while (cin >> s) {
    switch (s[0]) {
    case '+':
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(l + r);
      break;
    case '-':
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(r - l);
      break;
    case '*':
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(l * r);
      break;
    default:
      St.push(atoi(s.c_str()));
      break;
    }
  }

  cout << St.top() << endl;

  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>

using namespace std;

int main() {
  string s;
  stack<int> St;
  int l, r;
  while (scanf("%s", &s) != EOF) {
    if (s == "+") {
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(l + r);
    } else if (s == "-") {
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(l - r);
    } else if (s == "*") {
      l = St.top();
      St.pop();
      r = St.top();
      St.pop();
      St.push(l * r);
    } else {
      St.push(atoi(s.c_str()));
    }
  }

  printf("%d\\n", St.top());
  St.pop();

  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <ctype.h>
#include <iostream>
#include <stdexcept>
#include <stdlib.h>
#include <string>

template <class T> class Stack {
private:
  struct Node {
    Node *next;
    T data;

    Node(Node *next_ = NULL, T data_ = 0) : next(next_), data(data_) {}
  };

  int size_;
  Node *top;

public:
  Stack() : size_(0) {}
  ~Stack() {
    while (!isEmpty())
      pop();
  }

  void push(T data) {
    Node *node = new Node(top, data);
    top = node;
    size_++;
  }

  T pop() {
    if (isEmpty())
      throw std::runtime_error("nothing in stack");

    T data;
    Node *d = top;

    data = d->data;
    top = d->next;
    delete d;
    size_--;

    return data;
  }

  bool isEmpty() { return size_ == 0; }
};

int main() {

  Stack<int> st;
  std::string str;

  while (std::cin >> str) {
    if (isdigit(str[0])) {
      st.push(atoi(str.c_str()));
    } else {
      int x = st.pop();
      int y = st.pop();
      if (str == "+") {
        st.push(y + x);
      } else if (str == "-") {
        st.push(y - x);
      } else if (str == "*") {
        st.push(y * x);
      } else
        throw std::runtime_error("undefined operator '" + str + "'");
    }
  }
  std::cout << st.pop() << '\\n';

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::runtime_error'
  what():  undefined operator


Fixed submission:

#include <ctype.h>
#include <iostream>
#include <stdexcept>

template <class T> class Stack {
private:
  struct Node {
    Node *next;
    T data;

    Node(Node *next_ = NULL, T data_ = 0) : next(next_), data(data_) {}
  };

  int size_;
  Node *top;

public:
  Stack() : size_(0) {}
  ~Stack() {
    while (!isEmpty())
      pop();
  }

  void push(T data) {
    Node *node = new Node(top, data);
    top = node;
    size_++;
  }

  T pop() {
    if (isEmpty())
      throw std::runtime_error("nothing in stack");

    T data;
    Node *d = top;

    data = d->data;
    top = d->next;
    delete d;
    size_--;

    return data;
  }

  bool isEmpty() { return size_ == 0; }
};

int main() {

  Stack<int> st;
  char ch;

  while (std::cin >> ch) {
    if (isdigit(ch)) {
      st.push(ch - '0');
    } else {
      int x = st.pop();
      int y = st.pop();
      if (ch == '+') {
        st.push(y + x);
      }
      if (ch == '-') {
        st.push(y - x);
      }
      if (ch == '*') {
        st.push(y * x);
      } else
        throw std::runtime_error("undefined operator");
    }
  }
  std::cout << st.pop() << '\\n';

  return 0;
}