Trouble with char to string errors!!!

Hi there, I'm kind of a noob and have a software engineering assignment... Heres the question.

10 marks) Chapter 4, Programming 8, p. 168.
Write a C++ program that checks whether the bracketing operators (parentheses, brackets, and curly
braces) in a string are properly matched. As an example of proper matching, consider the string
{ s = 2 * (a[2] + 3 ); x = (1 + (2)); }
If you go through the string carefully, you discover that all the bracketing operators are correctly
nested, whith each open parenthesis matched by a close parenthesis, each open bracket matched by
a close bracket, and so on. On the other hand, the following strings are all unbalanced for reasons
indicated:
(([]) The line is missing a close parenthesis.
)( The close parenthesis comes before the open parenthesis.
{(}) The bracketing operators are improperly nested.
The reason that this exercise fits in this chapter is that one of the simplest strategies for implementing
this program is to store the unmatched operators on a stack.



Here is my code for what ive done so far.





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

//		
//	File: assignment3-Q1.cpp
//	---------------------------
//
//	This program follows the outlines in assignment 3

//			QUESTION 1


//Inclusions
#include "strutils.h"
#include "stack.h"
#include "genlib.h"
#include <iostream>
#include "simpio.h"

//Constants

//none

//Funtion Prototypes
bool checkBrackets(string str, int x);


//Main Program
int main(){

	string input_string= string("(Hey)(hello)");
	
	int y;
	y=input_string.length();
	cout<<checkBrackets(input_string,y);


	return 0;
}





//Function: checkBrackets
//This function takes in an equation as a
//string and checks if the bracketing is 
//correct as per the requirements in question
//1 in assignment 3.
//
bool checkBrackets(string str,int x){
	int i=0;
	Stack<string> bracketstack;
	while(i<=x){
		if (str.at(i)=='(')
				bracketstack.push(str.at(i));
				for (int j=i+1;i<=str.length();j++){
					if (str.at(j)==')'){
						bracketstack.pop();
						break;
					}
					else{
						bracketstack.push(str.at(j));
					}
				}
		i+=1;
	}

	if (bracketstack.isEmpty()){
		return true;
	}
	if (bracketstack.isEmpty()==false){
		return false;
	}
}



Assuming you're having problems with bracketstack.push(str.at(i));???

string::at() returns a char, so think you need to use Stack<char> ...???

Andy

P.S. Not sure where you're Stack come from, but there's a standard one in <stack> -- std::stack
Topic archived. No new replies allowed.