balances brackets problem(always the output is good)

I have to write a program which balances a string with brackets.I wrote the program but it doesn't matter which string I enter because the program always says that the string is good.
Here's the code:

header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef HEADER_H_
#define HEADER_H_
#include <string>

struct Element {
	char data;
	Element* link;
};

typedef Element* Stack;

void initStack(Stack& S);
void push(Stack& S, int a);
void pop(Stack &S);
int top(Stack& S);
bool isEmpty(Stack &S);
bool goodPair(char deschis, char inchis);
bool check(std::string s);

#endif

functions file
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
#include <iostream>
#include <string>
#include "header.h"
using namespace std;

void initStack(Stack& S)
{
	S = nullptr;
}

void push(Stack& S, int a)
{
	Element*nou = new Element;
	nou->data = a;
	nou->link = S;
	S = nou;
}

void pop(Stack& S)
{
	Stack aux = S;
	S = S->link;
	delete(aux);
}

int top(Stack& S)
{
	if (isEmpty(S))
		return int();
	return S->data;
}

bool isEmpty(Stack &S)
{
	if (S == 0)
		return true;
	else
		return false;
}

bool goodPair(char deschis, char inchis)
{
	if (deschis == '(' && inchis == ')')
		return true;
	else if (deschis == '[' && inchis == ']')
		return true;
	else if (deschis == '{' && inchis == '}')
		return true;
	else if (deschis == '<' && inchis == '>')
		return true;
	else
		return false;
}

bool check(std::string s)
{
	Element* S;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '(' || s[i] == '[' || s[i] == '{' || s[i] == '<')
			push(S, s[i]);
		else
		{
			if (s[i] == ')' || s[i] == ']' || s[i] == '}' || s[i] == '>')
				if (isEmpty(S) || !goodPair(top(S), s[i]))
					return false;
				else
					pop(S);
		}
	}
	if (isEmpty(S))
		return false;
	else
		return true;

}

main file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include "header.h"
using namespace std;

int main()
{
	Stack S;
	initStack(S);
	string s;
	cout << "Write the string:";
	cin >> s;
	if (check(s))
		cout << "Good";
	else
		cout << "Bad";
	return 0;

}


I used a stack and I traversed each character.If the character is an opening bracket I put it in the stack.When the character is a closing bracket, I compare it with the top of the stack.If it's good I pop the top of the stack.
Initialising your stack before pushing anything onto it might work.

The bare stack 'S' in check() isn't the same one you have in main.
You're right.I solved it.
Thank you:)
Just one question.If I replace std::string with string I get some errors.Why it's not working even if I wrote using namespace std?
> bool check(std::string s);
Presumably you're referring to this particular line.

Well the std namespace isn't yet evident to the compiler because that's in the header file, and your using statement is after that.

Ideally, you should be writing std::string and std::cin through your code.

Just saying "using namespace std;" is a lazy sop that allowed ancient C++ programs to use the standard headers without massive rewriting of the code.

Much like the special case of main assuming "return 0" as a sop to the void main programmers so all the poor dears had to do was change one word in their programs (like that was the only possible mistake to fix).

You could take a more measured approach by say
using std::cin;
using std::cout;


Do NOT be tempted to put "using namespace std;" in your header files. People will curse you.
I understood.Thank you!
Topic archived. No new replies allowed.