Put brackets in ArrayStack problem

Hello!

So I'm trying to put opening brackets in 1 arrayStack and closing brackets in Second ArrayStack. I use a normal char array to put the equation for example (2+2) then go through the normal char array and if it sees a opening bracket such as '(' it should put it in the array stack.

The program starts, but when i try to print information, nothing shows up and im not sure why. Any advice would be appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  using namespace std;

const int NUM = 100;


template <typename T>
class ArrayStack {
private:
	int n;
	T* arr;
	int t;
public:
	ArrayStack(int cap = NUM) {
		n = cap;
		arr = new T[n];
		t = -1;
	}

	

	void push(const T& obj) {
		if (size() == n) {
			cout << "Stack overflow" << endl;
			return;
		}

		t++;
		arr[t] = obj;
	}

	T pop() {
		if (isEmpty()) {
			cout << "Access to empty stack" << endl;
			return 1;
		}

		return arr[t--];
	}

	T& top() const{
		return arr[t];
	}

	bool isEmpty() const {
		return (t < 0);
	}

	int size() const {
		return(t + 1);
	}

	void print() {
		while (!isEmpty()) {
			cout << top();
			pop();
		}
		cout << endl;
	}
};

int main()
{
	
	
	char a[15];
	ArrayStack<char>* arr1 = new ArrayStack<char>(100);
	ArrayStack<char>* arr2;

	cout << "Please write an equation that you wish: " << endl;
	
	
	for (int i = 0; i < 15; i++) {
		cin >> a[i];
		
	}
	
	for (int c = 0; c < 15; c++) {
		if (a[c] == '(') {
			arr1->push(a[c]);
		
		}
	}

	arr1->print();
}




Thanks again !
1
2
3
4
for (int i = 0; i < 15; i++) 
{
		cin >> a[i];
}


This will loop until you have input 15 characters. Are you putting in 15 characters?
Ahhh, I only input 5 characters.

Then do i have to set instead of 15 to be the seize of the array ?

After i input 15 it actually returns properly haha :D
Just cin >> a;. No loop.

You're using a char array, which is dangerous and bad. You should be using a string.
Ahh i understand, i made it with string and the way you explained, it works now fine. Thanks again ! :) 0
Topic archived. No new replies allowed.